+ 4
Can a function be called from another function that is not main?
16 odpowiedzi
+ 7
Yes, you can even call the function itself. However, this may cause an infinite loop if there is no break condition. This is called recursion.
+ 1
yes
+ 1
i'm sure that's not what you want but.
#include <iostream>
using namespace std;
void simple()
{
cout<<"easy"<<endl;
}
int main()
{
simple();
}
0
yes
0
yes the fuction can be called from another function but if a function calls itself it's recursion...
0
yes you can
0
yes you can
0
of course :)
0
Yes
0
Yes ,
In recursion you can call..
0
yes , you can call .. to prevent it from infinite loop use break
0
I think it's c++
0
yes you can but define the function on up of other function
- 1
sure!!
have a nice day and check out my codes ;)
- 1
yes sure for example:
int f(int n){
if(n == 1||n==2)
return 1;
return (f(n-1)+f(n-2));
}
int main(){
cout << f(4);
return 0;
}
this is Fibonacci 4
- 2
yes it can, it has two ways:
1) the "called function" is defined/written/coded before the "caller function"
example:
float pi()
{
return 3.1412; //or some other code
}
float area( float radius)
{ return radius*radius*pi(); }
2) a prototype is declared in the "caller function", if this approach is taken, the "called function" can be anywhere in the program.
float area (float radius)
{ float pi();
return radius*radius*pi(); }
float pi()
{ return 3.1412; }
note: if function pi had parameters, its prototype would be:
float pi(int);
float pi(float);
would be like telling it: "there is a function called pi somewhere in the program and it takes an int/float/..."
as for the "function calling itself" mentioned by an earlier answer, that has special rules and is called as "recursion".