1

Can someone please explain to me how this prints 10 9 8 7...1 and not just 10?

cout<<b[x]<<endl; is inside if(x<y), so how come does it not terminate after x reaches 9, making it print 10 only?

#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; } } 
3
  • What's confusing me is that how does it print from 10 to 1 and not just 10? Commented Jun 27, 2021 at 23:21
  • The function does not terminate after the condition fail. It returns. When it returns it goes back to where it was called. Commented Jun 27, 2021 at 23:21
  • When it returns it goes back to where it was called from--and there's code after that. Commented Jun 27, 2021 at 23:24

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much You made it really easy to understand.. I've been trying for two hours

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.