What is the output of this question? I dont know how to solve this question please explain me | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the output of this question? I dont know how to solve this question please explain me

https://code.sololearn.com/cKem0r09Ru0H/?ref=app

11th Oct 2020, 2:24 PM
Vikramjeet Singh
Vikramjeet Singh - avatar
9 Answers
+ 7
This is recursion program and function calling again and again when u passing 5 in function it will check condition if condition is true then if statement will work if false then else will execute if part will be execute when number ==0 but in else part again function calling and it will decrease by 1 all times return will print values after one by one
11th Oct 2020, 2:32 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 3
Each recursive call creates a new copy of that method in the memory. Once some data is returned by the method, the copy is removed from the memory. Since all the variables and other stuff declared inside function get stored in the stack, therefore a separate stack is maintained at each recursive call. Once the value is returned from the corresponding function, the stack gets destroyed. Recursion involves so much complexity in resolving and tracking the values at each recursive call. Therefore we need to maintain the stack and track the values of the variables defined in the stack this recursive function for n =5. First, all the stacks are maintained which prints the corresponding value of n until n becomes 0, Once the termination condition is reached, the stacks get destroyed one by one by returning 0 to its calling stack.
11th Oct 2020, 7:17 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
To correct it call prinf before you call loop(i-1). Step 1-You call loop(5). Step 2-Go else... Then it call loop(4) Step 3-Then go else again and call 4-loop(3)... Up to i==0 and print(0). But all got stack, remember all print on else? Command continue. Then back where did you called. Printing 1... Back Step 3, printing 3. Then back step 2 printing 4. Then back step 1 printing 5.
12th Oct 2020, 1:35 AM
Marcelo Anjos
Marcelo Anjos - avatar
+ 2
To unserstand it try: #include <stdio.h> void callingOne() { printf("Called one\n"); } void callingTwo() { printf("Before calling One\n"); callingOne(); printf("After calling one\n"); } int main() { printf("Before call two\n"); callingTwo(); printf("After call two\n"); return 0; }
12th Oct 2020, 1:41 AM
Marcelo Anjos
Marcelo Anjos - avatar
+ 2
So, start at main. Main call callingTwo. Calling two star running and call callingOne Calling one start running. When it end, it back where was called, at callingTwo. Then callingTwo end and back to main.
12th Oct 2020, 1:46 AM
Marcelo Anjos
Marcelo Anjos - avatar
+ 1
Any one can clear this doubt?
11th Oct 2020, 7:07 PM
Vikramjeet Singh
Vikramjeet Singh - avatar
0
When loopy(i-1); Printf(i); I think print values are 54321
11th Oct 2020, 5:06 PM
Vikramjeet Singh
Vikramjeet Singh - avatar
0
But output are 012345
11th Oct 2020, 5:07 PM
Vikramjeet Singh
Vikramjeet Singh - avatar
0
Basically doubt not clear
11th Oct 2020, 5:08 PM
Vikramjeet Singh
Vikramjeet Singh - avatar