• 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

return statement

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

any idea why this would return "Sum = 15" and not "Sum = 1"?



couldn't find a similar thread with my brief search in the forum.
Thanks.
 
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was wrong, so I deleted this post. Don't want false information floating out there confusing everyone! See below post for correct answer.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Calculate calls itself recursively, so:
Calculate(5) = Calculate(4) + 5
= Calculate(3) + 4 + 5
= Calculate(2) + 3 + 4 + 5
= Calculate(1) + 2 + 3 + 4 + 5
= 1 + 2 + 3 + 4 + 5
= 15
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question. Please QuoteYourSources.
 
Jim Colwood
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies guys.

Yes Joe, I had my doubts about your reply. so I added couple of lines in the code to see what's going on at runtime.



Ruban, your calculation is right. but the real question i have is, even if the FIRST return statement returns the int, why is the returned value 15?

I had a question with similar logic in a Java test offered to me by a recruiter.
Thanks again.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruban, your calculation is right. but the real question i have is, even if the FIRST return statement returns the int, why is the returned value 15?



The FIRST return statement did return an int, but not to the main() method -- it returned it back to the calculate() method, which called it. If the main() method had triggered the FIRST return statement, then it would have printed 1.

Henry
 
Jim Colwood
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duh! it seems simple after you explained it, but it's easy to miss it.

Thanks a lot Henry.
 
reply
    Bookmark Topic Watch Topic
  • New Topic