defining functions

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Sun Feb 10 22:15:51 EST 2002


----- Original Message ----- 
From: "Patio87" <patio87 at aol.com>


> 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?

At this point, you are just *defining* the function.  Though it
is inaccurate, you may think of 'n' as a placeholder:

Python 2.1.2 (#1, Jan 16 2002, 17:04:23)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> def factorial(n):
...     if n == 1:
...         return 1
...     else:
...         return n * factorial(n-1)
...
>>> print factorial(5)
120
>>>

When you define the function, the question of "what is assigned to
'n'" is irrelevant.  When you *call* the function, *that's* when it
matters, and *that's* when it's obvious (5 in the case above).







More information about the Python-list mailing list