• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

order required

 
Ranch Hand
Posts: 401
Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is absolutely correct to call Mr. fred rosenberger
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic