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:

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 :icon_confused: ? Is anyone able to explain?

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

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:

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

System.out.println(f);

Alternatively, you could do it all in one line:

System.out.println(fact(4));
Comments
good answer, good information!

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.

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".

Comments
Thank you!

Also you got one staetment that causes your program to loop infinitely
it would be the x = 5;

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);               
            }
    }
Comments
Thank you!

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 guessing that it calls fact(x) until it reaches 1?

Yup, that's the basic premise of recursion.

This article has been dead for over six months. Start a new discussion instead.