Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!

# Introduction and a question about recursion

Patrick de Kruijf
Ranch Hand
Posts: 63
Hello there,

This is my first post at this forum, always an exciting moment. As part of my study I need to learn (the basics i guess) Java, and thats fantastic. I have to work through Headfirst Java and a some other reader.

The first 4 chapters of Headfirst went ok, it was hard but finally I understood what they are trying to teach me. But now in chapter 5 thing are going the other way. Since my study is a homestudy (to busy to go to school, kids and stuff) It's hard to get in contact with my teachter, they only check my homework, wich I send them through mail, and that's about all the contact I have. So here I am posting my first question, and I'm sure it won't be the last.

In the reader I got from school, there is a class that shows me something about recursion. Here is the code:

I have some questions about the final recursive part between the final curly braces. I guess the first line declares a long primitive with the name factorial which is casted into an int called n.

The second line says: if n is equal to 0 return 1. Question: But where does that 1 returns to?

The third and fourth line are a mystery to me: Else return N x factorial (n-1); Question: How do I have to read that? How does this return work? Why is (n-1) typed after the long primitive factorial? What does it do?

Thanks you very very much for your help!

Patrick

Hunter McMillen
Ranch Hand
Posts: 492
If you are having trouble reading it in the program I would suggest doing a few of these by hand.

Try calculating:
7!
10!
and
15! by hand (! means factorial).

the first line

is a method declaration with consists of a return type (long in this case) a name (factorial in this case), and a list of parameters (values that are being passed into this function). int n is merely a placeholder for an integer value entering your function.

the second line:

returns the value of 1 to where the method was called. This is called a base case, it is what stops a recursive function from executing.

The third and fourth lines are more complex:

means that you return n(the value you passed to the function) * the result of the function with the value (n-1) passed in.
so if I called factorial(5): it would result in factorial(5) + factorial(4) + factorial(3) + factorial(2) + 1

Hope this helps,
Hunter

Patrick de Kruijf
Ranch Hand
Posts: 63
The myst is clearing up a bit, thanks for that.

You say returns the value of 1 to where the method was called. Does this mean that factorial = 1 ? or n = 1? Or both?

Hunter McMillen
Ranch Hand
Posts: 492
Patrick de Kruijf wrote:The myst is clearing up a bit, thanks for that.

You say returns the value of 1 to where the method was called. Does this mean that factorial = 1 ? or n = 1? Or both?

This means that when n = 1, your factorial function will return the literal value of 1. The value displayed on your screen will likely be a larger number, but if you look at all the recursive steps you get this for factorial(5):

5x4x3x2x1 = 120

Hunter