defining functions

Jonathan Gardner jgardn at alumni.washington.edu
Mon Feb 11 08:42:50 CET 2002


Patio87 wrote:
> 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?

Remember algebra, or crack open an algebra book if you don't remember.
 
What does "f(x) = 4x" mean?

Asking what "n" represents is like asking what "x" represents in my 
example. You don't know until you actually *do* something like f(4).

By the same token, factorial(4) means "evaluate the factorial code, with 
n=4". When you end up calling factorial(n-1) at the bottom, you will 
evaluate the same function again with n=3, because 4-1 is 3.

Jonathan





More information about the Python-list mailing list