I am currently programming some division-conquest algorithms where recursion functions are used everywhere, but I have a very vague idea of โโhow this works and why I post it here and hope you don't mind the main one either.
For example, if we have the following code:
#include<iostream> using namespace std; void Recursion(int n) { cout << n << endl; if(n > 0) { Recursion(n-1); } cout<<n<<endl; } int main() { Recursion(3); return 0; }
I tested recursion (3) and the printout in the terminal:
3 2 1 0 0 1 2 3
I can understand the concept of a recursive function call, but I donโt understand how it works. For example, what will they do after they cannot call the function again? For example, here I can understand that it prints from 3 to 0, but why does it also print from 0 to 3 again? I heard this because the recursion function is stored on the stack for one recursion, and when it reaches the "bottom", it must also be deleted.
But anyway, I donโt know about it. So, can someone help me and clearly tell me what happened here and the exact flow of the function call?
Thank you for your help!
source share