Can someone explain me this code ??? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Can someone explain me this code ???

It is an example of recursive function from learncpp.com . I can't understand why the output includes this: pop 2 pop 3 pop 4 pop 5 Website link: http://www.learncpp.com/cpp-tutorial/7-11-recursion/

25th Feb 2018, 7:46 AM
Junaid Siddiqui
Junaid Siddiqui - avatar
6 Answers
+ 6
Is there a reason why you wouldn't expect these to output, considering there is a line printing these to the output stream? What behavior were you expecting with the pop messages? Remember, each recursive call will [first] execute everything above the recursive call line. After the final recursive call is completed, execution will proceed below the recursion line for each call on the stack. [UPDATED: Added word in bracket for clarity.]
25th Feb 2018, 8:09 AM
David Carroll
David Carroll - avatar
+ 9
@David "After the final recursive call is completed, execution will proceed below the recursion line for each call on the stack." Ok I understood it . Thanks!!! This is a property of every recursive function right ?
25th Feb 2018, 9:13 AM
Junaid Siddiqui
Junaid Siddiqui - avatar
+ 7
@Junaid... As this code was written, that is the behavior. If you were attempting a different behavior, let me know and I can help with what needs to be modified.
25th Feb 2018, 2:28 PM
David Carroll
David Carroll - avatar
+ 6
@David I was expecting this output: First all push statements pop 1 Just this. But I think I understood it now properly. Watched some videos on YouTube. Thanks for helping brother!!😀
25th Feb 2018, 5:16 PM
Junaid Siddiqui
Junaid Siddiqui - avatar
+ 6
Gotcha... I'm glad I could help.
25th Feb 2018, 5:59 PM
David Carroll
David Carroll - avatar
+ 1
one time return 1,
25th Feb 2018, 12:56 PM
Czilla
Czilla - avatar