Wednesday, August 28, 2002 02:19 AM
>>> So when value of "i" reaches to "0" as per my knowledge ,it should return to main() method...
Not really! Your display() methods checks that if i==0 then "return", nowhere does it says "return to main()". It cant actually say that. So, as it happens in normal course, control will return to the statement which is next to the statement from where this function was called. In this case it is the println() statement (if i is 0).
>>>Why it again enters in to the method display() after exiting that method..
It is NOT entering the display() method again. All it is doing is executing the statements which are just after the call to display(). You can if you wish, check this by putting a println("entered display method with i="+i) as the very first line of the display() method, even before you check i==0.
Here is what is going on in detail. Your main calls display() with i=10. display() checks wether i==0, if not it calls display() with i=9. Again since i is not 0, it will call display() with i=8 and so on. Finally, a display() with i==0 will be called, from display(1). Since i is 0, this function returns control to the statement which is just after the display(i-1) statement. Here you have the println() statement, which would fire for i=1 and hence print a (that is a[i-1]). Then it would meet the ending curly brace of display(1), and return the control back to the statement which was next to display(2). This again is the println() and prints a. This cycle would continue till all calls are finished, and finally the display() method finishes. So the output you are getting (printing of all values from a to a) is infact correct and expected.
Hope that helps.