+ 4

Can a function be called from another function that is not main?

3rd Jan 2016, 4:48 PM
Riccarda
Riccarda - avatar
16 Réponses
+ 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.
3rd May 2016, 7:06 PM
wisdom123
wisdom123 - avatar
+ 1
yes
3rd Dec 2016, 7:42 AM
Shekhar Bahuguna
Shekhar Bahuguna - avatar
+ 1
i'm sure that's not what you want but. #include <iostream> using namespace std; void simple() { cout<<"easy"<<endl; } int main() { simple(); }
6th Dec 2016, 7:07 PM
Wheres8
Wheres8 - avatar
0
yes
3rd Jan 2016, 7:23 PM
Avinash More
0
yes the fuction can be called from another function but if a function calls itself it's recursion...
2nd Dec 2016, 2:56 AM
Rahul Sharma
Rahul Sharma - avatar
0
yes you can
6th Dec 2016, 12:39 PM
Nicoleta Dumitru
Nicoleta Dumitru - avatar
0
yes you can
7th Dec 2016, 8:19 PM
alireza
alireza - avatar
0
of course :)
8th Dec 2016, 11:41 PM
Adrian Gorea
Adrian Gorea - avatar
0
Yes
11th Dec 2016, 9:03 AM
Agaba Ivan
Agaba Ivan - avatar
0
Yes , In recursion you can call..
13th Dec 2016, 1:58 PM
Dixem
Dixem - avatar
0
yes , you can call .. to prevent it from infinite loop use break
13th Dec 2016, 7:16 PM
Mock
Mock - avatar
0
I think it's c++
14th Dec 2016, 9:35 AM
Amini Amini
Amini Amini - avatar
0
yes you can but define the function on up of other function
23rd Jul 2017, 12:54 PM
alireza
alireza - avatar
- 1
sure!! have a nice day and check out my codes ;)
2nd Dec 2016, 9:57 PM
Alessandro Ampala
Alessandro Ampala - avatar
- 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
3rd Dec 2016, 12:21 PM
alireza
alireza - avatar
- 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".
25th Jun 2016, 9:52 PM
special