Return Statements. What's supposed to happen?

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Return Statements. What's supposed to happen?

 
0
  #1
Mar 16th, 2008
I have been trying to write simple return statements and print the result of the return. I have read books such as O-Reily and have used examples but find that the examples don't work either so I guess I just need human communication!!

First I have tried this return method - no errors:

  1.  
  2. public class recursiveTest {
  3. public static int fact(int x)
  4. {
  5. x = 5;
  6. if(x==1 || x==0) return x;
  7. else {
  8. return x * fact(x-1);
  9. }
  10. }
  11. public static void main(String args[]){
  12. }
  13. }

Isn't the return supposed to print to system out? If it isn't I also wrote a method to print factorial(x) but that is usually an unreachable statement and a missing return. So how do you print the return value ? Is anyone able to explain?
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Return Statements. What's supposed to happen?

 
0
  #2
Mar 16th, 2008
for a start, your main method (entry point) has no statements, therefore the program will not execute as you expect. Show us some more code and I will help you further if I can
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 262
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Return Statements. What's supposed to happen?

 
1
  #3
Mar 16th, 2008
The return value gets passed back to the calling method. You could do anything with the returned value.

To print the return variable, you may want to add the following line to your main method:

  1. int f = fact(4);

which will call your factorial method. When the method completes, it will return the value completed to the calling method (in this case the main method) where it assigns the returned value to an integer variable called f.

The next line to get it to print then would be

  1. System.out.println(f);

Alternatively, you could do it all in one line:

  1. System.out.println(fact(4));
It is very important to read this: http://www.catb.org/~esr/faqs/smart-questions.html
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: Return Statements. What's supposed to happen?

 
0
  #4
Mar 16th, 2008
Thanks a lot, return methods kind of represent the value of the method then...But what should I do when the fact(4) statement prints 'at recursiveTest.fact(recursiveTest.java:31)' ?

That's all of the code - it's just a small example to understand how recursion/return values works.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 262
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Return Statements. What's supposed to happen?

 
1
  #5
Mar 16th, 2008
I haven't done a lot of work in Java recently, so I'm not 100% on this, but try taking out the "static" in the fact method. (If that doesn't work, add the "static" back in and report back to us!)

Are you sure all you're getting out of it is "at recursiveTest.fact(recursiveTest.java:31)" that sounds like the tail end of a stack trace after a Java exception occurs. There should be a little bit more information before the "at".
Last edited by Dukane; Mar 16th, 2008 at 9:28 pm.
It is very important to read this: http://www.catb.org/~esr/faqs/smart-questions.html
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 13
Reputation: shinnxennosagga is an unknown quantity at this point 
Solved Threads: 1
shinnxennosagga shinnxennosagga is offline Offline
Newbie Poster

Re: Return Statements. What's supposed to happen?

 
1
  #6
Mar 16th, 2008
Also you got one staetment that causes your program to loop infinitely
it would be the x = 5;
  1. public static int fact(int x)
  2. {
  3. x = 5; //this part will set x to value 5
  4. if(x==1 || x==0) return x; //so, because x is always 5, this conditional would never fullfilled
  5. else {
  6. return x * fact(x-1);
  7. }
  8. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 28
Reputation: Cleo123 is an unknown quantity at this point 
Solved Threads: 0
Cleo123 Cleo123 is offline Offline
Light Poster

Re: Return Statements. What's supposed to happen?

 
0
  #7
Mar 19th, 2008
Thanks a lot guys, it now work and returns 24. If I took the static out it says a non static method cannot be referenced from the static method - makes sense - must be from static to static. I didn't get the beging for the error message before 'at' because as shin' said it was looping and so DOS cuts off the beginning.

I'm really impressed with how it works without a do while/ loop because it's taking away 1 each time to find factorial...quite unsure about that worked (?) but I figured that I didn't need to initialize x to anything because whatever number i put into fact(x) would just override it anyway.

I'm guessing that it calls fact(x) until it reaches 1?

Thanks again for solving the problem(s) and sorry for the late reply I've been doing my 'real' java coursework!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 262
Reputation: Dukane is an unknown quantity at this point 
Solved Threads: 22
Dukane's Avatar
Dukane Dukane is offline Offline
Posting Whiz in Training

Re: Return Statements. What's supposed to happen?

 
0
  #8
Mar 20th, 2008
Originally Posted by Cleo123 View Post
I'm guessing that it calls fact(x) until it reaches 1?
Yup, that's the basic premise of recursion.
It is very important to read this: http://www.catb.org/~esr/faqs/smart-questions.html
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC