• 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

When A Method Returns?

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does it mean to say 'when a method returns'? I think i have a vague idea but maybe someone can clarify this once and for all for me. Thanks in advance.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call a method, program execution leaves the caller, and goes to that method. At some point, the method will finish, and then control "returns" back from where it originally came.
for example, two methods:
public void callingMethod()
{
doMethodCall(); //at this line, program execution "jumps" to the doMethodCall() method
}
public void doMethodCall()
{
//statements....
//when there are no more method statements, the method is done, so it returns. In this case, it returns back to the callingMethod(), since that is the method that called this one.
//You can also return explicitly, with a return statement:
return; //this immediately returns control to the caller.
}
If you have a method that declares that it returns a value, you put the value you are returning to the right of the return statement.
public int howManyFingersOnOneHand()
{
return 5; //control returns , AND the value 5 is returned to the calling method.
}
if I called the above method like so:
int fingerCount = howManyFingersOnOneHand();
the value 5 is returned from the method, and stored in the variable fingerCount.

I hope this helps!
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe instead of using the phrase 'when the method returns' think of it as 'when the method is done executing'.
 
ernest fakudze
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you so much for explaining this to me. It's that simple! Now I know and I'm happy . Really appreciate guys!!
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic