Try thinking of this in terms of code substituation.
At each point that someFunction() is called swap in the actual code of the function.
include <iostream> using namespace std; void someFunction(int[], int, int); int main() { int a[]={1,2,3,4,5,6,7,8,9,10}; int value=10; someFunction(a,0,value); return 0; } void someFunction(int b[], int x, int y){ if(x<y){ someFunction(b,x+1,y); cout<<b[x]<<endl; } }
Lets look at the one line in main that matters:
someFunction(a,0,10);
Step 1: Replace this by the function body (replacing the variables with the actual paramters)
if(0<10){ someFunction(a,1,10); cout<<a[0]<<endl; }
Step 2: That if statement is always true so lets remove it:
someFunction(a,1,10); cout<<a[0]<<endl;
Repeat Steps 1
if(1<10){ someFunction(a,2,10); cout<<a[1]<<endl; } cout<<a[0]<<endl;
Repeat Steps 2
someFunction(a,2,10); cout<<a[1]<<endl; cout<<a[0]<<endl;
We still have a call to someFunction() so lets repeat again:
if(2<10){ someFunction(a,3,10); cout<<a[2]<<endl; } cout<<a[1]<<endl; cout<<a[0]<<endl;
And:
someFunction(a,3,10); cout<<a[2]<<endl; cout<<a[1]<<endl; cout<<a[0]<<endl;
OK. You see the pattern. This repeats until you get to fail condition.
someFunction(a,10,10); cout<<a[9]<<endl; cout<<a[8]<<endl; cout<<a[7]<<endl; cout<<a[6]<<endl; cout<<a[5]<<endl; cout<<a[4]<<endl; cout<<a[3]<<endl; cout<<a[2]<<endl; cout<<a[1]<<endl; cout<<a[0]<<endl;
Now if we replace that last call
if(10<10){ someFunction(a,11,10); cout<<a[10]<<endl; } cout<<a[9]<<endl; cout<<a[8]<<endl; cout<<a[7]<<endl; cout<<a[6]<<endl; cout<<a[5]<<endl; cout<<a[4]<<endl; cout<<a[3]<<endl; cout<<a[2]<<endl; cout<<a[1]<<endl; cout<<a[0]<<endl;
This time we see that the condition is false. So we can remove it and the code.
cout<<a[9]<<endl; cout<<a[8]<<endl; cout<<a[7]<<endl; cout<<a[6]<<endl; cout<<a[5]<<endl; cout<<a[4]<<endl; cout<<a[3]<<endl; cout<<a[2]<<endl; cout<<a[1]<<endl; cout<<a[0]<<endl;
That's it.
returns. When it returns it goes back to where it was called.