83
Functions
63
def Statements with Parameters
When you call the
print()
or
len()
function, you pass in values, called argu-
ments in this context, by typing them between the parentheses. You can also
define your own functions that accept arguments. Type this example into
the file editor and save it as helloFunc2.py:
u def hello(name):
v print('Hello ' + name)
w hello('Alice')
hello('Bob')
When you run this program, the output looks like this:
Hello Alice
Hello Bob
The definition of the
hello()
function in this program has a parameter
called
name
u. A parameter is a variable that an argument is stored in when a
function is called. The first time the
hello()
function is called, it’s with the
argument
'Alice'
w. The program execution enters the function, and the
variable
name
is automatically set to
'Alice'
, which is what gets printed by the
print()
statement v.
One special thing to note about parameters is that the value stored
in a parameter is forgotten when the function returns. For example, if you
added
print(name)
after
hello('Bob')
in the previous program, the program
would give you a
NameError
because there is no variable named
name
. This
variable was destroyed after the function call
hello('Bob')
had returned, so
print(name)
would refer to a
name
variable that does not exist.
This is similar to how a program’s variables are forgotten when the pro-
gram terminates. I’ll talk more about why that happens later in the chapter,
when I discuss what a function’s local scope is.
return Values and return Statements
When you call the
len()
function and pass it an argument such as
'Hello'
,
the function call evaluates to the integer value
5
, which is the length of the
string you passed it. In general, the value that a function call evaluates to is
called the return value of the function.
When creating a function using the
def
statement, you can specify what
the return value should be with a
return
statement. A
return
statement con-
sists of the following:
• The
return
keyword
• The value or expression that the function should return