+ 3
What's it's ouput? Please explain briefly.
void func(int x) { if(x>0) { func (--x); cout<<x; } else return; } int main() { func(5); return 0; }
7 Antworten
+ 3
01234. Is the output after fixing semicolon.
The function call can be shown as :
fun(5) - > fun(4) - >fun(3) - > fun(2)-> fun(1) - > fun(0) - > return
The function are called in this way. We have a stack of function call which means that we return from func(0) then 1, then 2 and so on.
Base case: x <= 0
So function call terminates/ returns at fun(0)
Then we backtrack to the position which called func (0) i.e func(1) displaying 0
func (2) displaying 1
fun(3) displaying 2.. all the way to func(5) displaying 4.
The numbers are printed as 01234 because the cout statement is after each function call.
+ 3
This is a recursive function.
Output should be something like:
01234
Why not run the code to check?
semicolon missing after func(x--)
+ 3
A compile error because you are missing the ; after func (--x). Once fixed, it outputs 01234. func is called with 5, 4, 3, 2, 1, and 0. 0 just returns. The rest decrements x, calls the function, and prints x so the call with 1 gets to be the first to output it's 0.
+ 2
VapeHorization plzz elaborate
+ 1
You don’t need return because the type of function is Void.
If argument named x is greater than x, decrement and then print it. If not, return ( mistake )
+ 1
Oh sorry I believed it was if (x<5), my mistake ^^
+ 1
Xan bro I know the answer but I want to know how this program executed