| | |
Return Statements. What's supposed to happen?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
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:
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?
First I have tried this return method - no errors:
Java Syntax (Toggle Plain Text)
public class recursiveTest { public static int fact(int x) { x = 5; if(x==1 || x==0) return x; else { return x * fact(x-1); } } public static void main(String args[]){ } }
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
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!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
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:
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
Alternatively, you could do it all in one line:
To print the return variable, you may want to add the following line to your main method:
java Syntax (Toggle Plain Text)
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)
System.out.println(f);
Alternatively, you could do it all in one line:
java Syntax (Toggle Plain Text)
System.out.println(fact(4));
It is very important to read this: http://www.catb.org/~esr/faqs/smart-questions.html
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".
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
•
•
Join Date: Feb 2008
Posts: 13
Reputation:
Solved Threads: 1
Also you got one staetment that causes your program to loop infinitely
it would be the x = 5;
it would be the x = 5;
Java Syntax (Toggle Plain Text)
public static int fact(int x) { x = 5; //this part will set x to value 5 if(x==1 || x==0) return x; //so, because x is always 5, this conditional would never fullfilled else { return x * fact(x-1); } }
•
•
Join Date: Nov 2007
Posts: 28
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
- How to be Crash Free (C++)
Other Threads in the Java Forum
- Previous Thread: How to kill the running .jar file from another jar in java?
- Next Thread: GUI problem
| Thread Tools | Search this Thread |
6 actuate android api applet application array arrays automation balls binary bluetooth bold business c++ chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging ebook eclipse educational error event exception file fractal froglogic game givemetehcodez graphics gui helpwithhomework hql html ide ideas image ingres input integer internet intersect invokingapacheantprogrammatically j2me java javaexcel javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie nextline parameter php print problem program programming project recursion recursive scanner screen sell server set size sms sort sql string sun swing swt threads tree user websites windows






