This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

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

------------------8<------------------
please cut here

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

As to your other questions:
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

"If the facts don't fit the theory, get new facts" --Albert Einstein

Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63

posted

0

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?

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):