[Tutor] Newbie Question: Define Funcations
alan.gauld@bt.com
alan.gauld@bt.com
Sun, 19 May 2002 17:46:41 +0100
> From my understanding, the parameter is the name used inside
> a function to refer to the value passed as an argument. Example:
Well done some programmers take years to appreciate that subtlety!
> def printtwice(bruce):
> print bruce, bruce
>
> printtwice(spam)
> Traceback (most recent call last):
> File "<pyshell#3>", line 1, in ?
> printtwice(spam)
> NameError: name 'spam' is not defined
>
> Do I have to define the argument before I use it in a function
No you could pass a value directly. But yuou have passed an undefined
name to python. I suspect you meant to pass the string value 'spam'
(Note the quotes...)
printtwice('spam')
Or you could assign spam, thus:
spam = 'spam'
printtwice(spam)
Either will achieve what you want.
Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld