Function Parameters
Some of the built-in functions we have seen require arguments. For
example, when you call math.sin you pass a number as an argument.
Some functions take more than one argument: math.pow takes two,
the base and the exponent.
Inside the function, the arguments are assigned to variables called parameters. Parameters are specified within the pair of parentheses in the function definition, separated by commas. When we call the function, we supply the values in the same way.
Note the terminology used - the names given in the function definition are called parameters whereas the values you supply in the function call are called arguments.
Here is an example of a user-defined function that has a parameter and takes an argument:
| Code | Notes |
|---|---|
def print_twice(the_phrase): |
This function assigns the argument to a parameter named
|
This function works with any value that can be printed.
| Code | Output |
|---|---|
def
print_twice(the_phrase): |
Spam |
The same rules of composition that apply to built-in functions also apply to user-defined functions, so we can use any kind of expression as an argument for print_twice:
| Code | Output |
|---|---|
def
print_twice(the_phrase): |
SpamSpamSpamSpam |
The argument is evaluated before the function is called, so in the examples
the expressions "Spam"*4 and max("Spam!") are only
evaluated once.
You can also use a variable as an argument:
| Code | Output |
|---|---|
def
print_twice(the_phrase): |
I'm not dead yet |
The name of the variable we pass as an argument (status) has
nothing to do with the name of the parameter (the_phrase). It
doesn’t matter what the value was called back home (in the caller); here in
print_twice, we call everything the_phrase.
Example: Function with Multiple Parameters
def print_max(a,
b):
if a >
b:
print(a, 'is
maximum')
elif a ==
b:
print(a, 'is equal to',
b)
else:
print(b,
'is maximum')
# directly pass literal values
print_max(3, 4)
x
= 5
y = 7
# pass variables as arguments
print_max(x, y)
Output:
4 is maximum
7 is maximum
How It Works
Here, we define a function called print_max that uses two
parameters called a and b. We find out the greater
number using a simple if-else statement and then print the bigger
number.
The first time we call the function print_max, we directly
supply the numbers as arguments. In the second case, we call the function with
variables as arguments. print_max(x, y) causes the value of
argument x to be assigned to parameter a and the value
of argument y to be assigned to parameter b. The
print_max function works the same way in both cases.
Fruitful Functions and Void Functions
Some of the functions we are using, such as the math functions,
yield results; you can call them fruitful functions
or return-value function. Other functions, like
print_twice, perform an action but don’t return a value. They are
called void functions.
When you call a fruitful function, you almost always want to do something with the result; for example, you might assign it to a variable or use it as part of an expression:
| Code |
|---|
x = math.cos(radians) |
When you call a function in interactive mode, Python displays the result:
>>> math.sqrt(5)
2.2360679774997898
But in a script, if you call a fruitful function and do not store the result of the function in a variable, the return value vanishes into the mist!
| Code | Output |
|---|---|
math.sqrt(5) |
Nothing |
This script computes the square root of 5, but since it doesn’t store the
result in a variable or display the result using the print
statement, it is not very useful.
Void functions might display something on the screen or have some other
effect, but they don’t have a return value. If you try to assign the result to
a variable, you get a special value called None.
| Code | Output | Notes |
|---|---|---|
hi = print("Hello!") |
Hello |
In this case, we have assigned the variable The print statement performs an action. It displays the word
|
print(hi) |
None |
But when we try to display the value of the variable
|
The value None is not the same as the string
'None'. It is a special value that has its own type:
| Code | Output |
|---|---|
print(type(None)) |
<class 'NoneType'> |
To return a result from a function, we use the return statement
in our function. The return statement is used to return from a
function i.e. break out of the function. We can optionally return a value from
the function as well.
For example, we could make a very simple function called addtwo
that adds two numbers together and returns a result.
| Code | Output |
|---|---|
def addtwo(a, b):
|
8 |
When this script executes, the print statement will print out
"8" because the addtwo function was called with
3 and 5 as arguments. Within the function, the
parameters a and b were assigned to 3 and
5 respectively. The function computed the sum of the two numbers
and placed it in the local function variable named added. Then it
used the return statement to send the computed value back to the
calling code as the function result, which was assigned to the variable
x and printed out.
Example: Simple Return Statement
def maximum(x,
y):
if x >
y:
return
x
elif x ==
y:
return 'The numbers are
equal'
else:
return
y
print(maximum(2, 3))
Output:
3
How It Works
The maximum function returns the maximum of the parameters, in this case the
numbers supplied to the function. It uses a simple if-else
statement to find the greater value and then returns that value.
Note that a return statement without a value is equivalent to return
None. None is a special type in Python that
represents nothingness. For example, it is used to indicate that a variable has
no value if it has a value of None.
Every function implicitly contains a return None statement at
the end unless you have written your own return statement. You
can see this by running print(some_function()) where the function
some_function does not use the return statement such
as:
def some_function():
pass
As we have seen, the pass statement is used in Python to
indicate an empty block of statements.
TIP: As we have seen, there is a built-in function called max
that already implements the 'find maximum' functionality, so use this built-in
function whenever possible.
More about Return Values
You aren’t limited to returning numbers. You can return strings and Boolean values as well – and even other objects we haven't yet discovered like lists, tuples, and dictionaries just to name a few. Here are a few examples:
| Code | Output |
|---|---|
def is_blue(color): |
False |
def display_twice(word): |
Hi!Hi! |
Returning Multiple Values
A function can return more than one value by separating the individual return values by commas.
return value1, value2, value3, etc
When you call the function, you assign it to multiple variables as well:
| Code | Output |
|---|---|
def name(): |
Weasley |
In this example, the function name returned two values.
The line:
first_name, last_name = name()
assigned the first returned value to the variable first_name and
the second returned value to the variable last_name. You
have to pay attention to which order the values are returned so you assign them
to the correct variables when you call the function.
