943,917 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1101
  • Java RSS
Mar 16th, 2008
0

Return Statements. What's supposed to happen?

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 43
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
159 posts
since Nov 2007
Mar 16th, 2008
0

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

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
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Mar 16th, 2008
1

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

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:

java Syntax (Toggle Plain Text)
  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

java Syntax (Toggle Plain Text)
  1. System.out.println(f);

Alternatively, you could do it all in one line:

java Syntax (Toggle Plain Text)
  1. System.out.println(fact(4));
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Mar 16th, 2008
0

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

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.
Reputation Points: 43
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
159 posts
since Nov 2007
Mar 16th, 2008
1

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

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.
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006
Mar 16th, 2008
1

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

Also you got one staetment that causes your program to loop infinitely
it would be the x = 5;
Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 11
Solved Threads: 1
Newbie Poster
shinnxennosagga is offline Offline
13 posts
since Feb 2008
Mar 19th, 2008
0

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

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!
Reputation Points: 43
Solved Threads: 12
Junior Poster
StephNicolaou is offline Offline
159 posts
since Nov 2007
Mar 20th, 2008
0

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

Click to Expand / Collapse  Quote originally posted by Cleo123 ...
I'm guessing that it calls fact(x) until it reaches 1?
Yup, that's the basic premise of recursion.
Reputation Points: 45
Solved Threads: 28
Posting Whiz in Training
Dukane is offline Offline
282 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Jdbc-oracle
Next Thread in Java Forum Timeline: GUI problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC