[Tutor] Newbie Question: Define Funcations

Kalle Svensson kalle@lysator.liu.se
Sat, 18 May 2002 04:23:38 +0200


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[MBussell@aol.com]
> From my understanding, the parameter is the name used inside a
> function to refer to the value passed as an argument.  Example:
> 
>        def printtwice(bruce):
>              print bruce, bruce
> 
>        printtwice(spam)       
> Theoretical output:
>        spam spam
> 
> Actual output:
>        Traceback (most recent call last):
>   File "<pyshell#3>", line 1, in ?
>     printtwice(spam)
> NameError: name 'spam' is not defined

The thing is that spam hasn't been defined in the place where you call
printtwice():

>>> def printtwice(bruce):
...    print bruce, bruce
... 
>>> printtwice(spam)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'spam' is not defined
>>> spam = "Hello world" # Now give a value to spam
>>> printtwice(spam)
Hello world Hello world
>>> 

When you call the function printtwice, the Python interpreter tries to
find the value of the variable spam to send as an argument to the
function.  As it hasn't been given a value, there is an error message.

Peace,
  Kalle
- -- 
Kalle Svensson, http://www.juckapan.org/~kalle/
Student, root and saint in the Church of Emacs.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.6 <http://mailcrypt.sourceforge.net/>

iD8DBQE85bsjdNeA1787sd0RAj8vAKCevrQL9FHFI1K+utWHgY9U3bEcfQCfaRk4
yJnNdAYccAwqI2JzCVmQpcE=
=lJzj
-----END PGP SIGNATURE-----