defining functions

Michael Chermside mcherm at destiny.com
Mon Feb 11 15:59:29 EST 2002


 > I am pretty new to python, and I dont understand the parameters when 
defining
 > functions. Whenever I see a function definition with a argument It 
seems like
 > the argument already is a variable, but it goes threw the function 
and it makes
 > no sense. I dont know if what I just said made any sence but if you 
can help me
 > please reply
 > def factorial(n):
 >     if n == 1:
 >         return 1
 >     else:
 >         return n * factorial(n-1)
 > What the hell does 'n' have assingned to it?

I think that Antaeus Feldspar gave an *excellent* easy-to-understand 
answer to your question, but I wanted to add one more point that might 
help you out.

His explanation told how a method like this would work:

 >   def average_two(first_num, second_num):
 >       sum = first_num + second_num
 >       average = sum / 2.0  # Dividing by 2 would abandon any 
remainder
 >       return average

But your function (factorial) used a special feature called "recursion". 
Recursion is a really useful feature, but it makes it more difficult to 
answer the question "What the hell does 'n' have assigned to it?".

If you think of things the way that Antaeus explained them, you'll 
understand what the factorial() function does when you call it as 
"factorial(1)": It temporarily makes 'n' take on the value of 1, then it 
does the test 'if n == 1', which is TRUE, so it returns 1. But when you 
call "factorial(2)" it gets a lot harder to understand.

YOU could probably work it out in your head: Factorial of 1 is defined 
to be 1, and factorial of two is defined to be 2 * factorial of 1. So 
factorial(2) should be 2*1 or simply 2. Hey... in your head, you could 
probably do higher numbers the same way: Factorial of 3 is defined to be 
3 * factorial(2), which we ALREADY know is 2, so factorial of 3 must be 6.

If I were to ask you the factorial of 5 (and you didn't know it), then 
you'd have to start by working out the factorial of 4 (which is 4 * 
factorial of 3... ie 4 * 6 or 24), and then multiply THAT by 5 (so 5*24 
= 120). And that's exactly what the computer does.

Say you define the factorial function as above, and then type 
"factorial(5)" in at the command line. Well, of course (as Antaeus 
explained quite nicely) 'n' gets assigned the value 5. We run the test 
("if n == 1"), and it's FALSE so we do the later line: "return n * 
factorial(n-1)". The computer calculates n-1 (it's 4), but then needs to 
know what "factorial(4)" is.

So it starts the whole thing over. I "remembers" that it has to multiply 
by 'n' (which is 5) when it's done, and goes off to figure out what 
"factorial(4)" is. To do THAT is has to create a NEW 'n' which is equal 
to 4 and run through the whole calculation again. It keeps doing this 
until it gets to n=1 which (as we already know) is easy, and then it can 
work out all of the answers.

A few pieces of vocabulary. The "remembering" of the old value of n 
while it takes a break to work out "factorial(4)" is called a "stack 
frame". And the whole process is called "recursion".

Hope that helps!

-- Michael Chermside








More information about the Python-list mailing list