• Post Reply Bookmark Topic Watch Topic
  • New Topic

order required  RSS feed

 
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



may i know how it is printing 1,0

when f1(i) is called i=0, but it is calling f1(i) with i=0, and when controller comes back i=0 without even telling i's values.

please let me know how this program is giving 1,0 output.
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure I can explain in beginner's language, but basically it's all about order of operations and how stack-based computing works.

Here's what's going on:

Line 8: i is set to zero

Line 9: i = i++ + fl(i)

Evaluation order of the right-hand-side is left to right, so:

i++ : the value of i is pushed onto the stack, and then i is incremented by 1
i is now 1, and the stack is {0}

fl(i) is evaluated, with i equal to 1. This prints the "1," and then returns zero
The return value of the function is placed onto the stack

i is now 1, and the stack is {0, 0}

The addition operation is now evaluated, popping the first two values off of the stack and adding them together
0 and 0 are popped and added, resulting in a value of 0.

This value is pushed onto the stack as the total value of the right hand side.

The assignment then takes place, setting i to the value of the right hand side, which is 0
i is now 0, and the stack is irrelevant for our purposes.

We now print the value of i, which is 0.

End result is we've printed 1,0
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it is calling f1(i) with i=0


this is an incorrect statement..

 
Rauhl Roy
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is absolutely correct to call Mr. fred rosenberger
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!