In this lab, we introduced several major important topics in Python. Let’s quickly review them.
Python Functions
A function in Python is a piece of code that is given a name, which can then be run by calling it elsewhere in the program.
To create a function, we use the following structure:
def function_name(parameter1, parameter2):
<block of statements>
- Function names follow the same rules as variable names.
- Functions may require 0 or more parameters, placed in parentheses after the function name. Multiple parameters are separated by commas.
- When called, a function will run the lines of code indented beneath the function definition.
To call a function, we use the following structure:
function_name(argument1, argument2)
- A function call is the function name, followed by parentheses
()
. - Inside of the parentheses, a matching argument must be included for each parameter required by the function. Multiple arguments are separated by commas.
- Each argument may be an expression that evaluates to a value.
- The values of each argument are copied into the function. When the function ends, the original arguments are unchanged.
Returning Data from Functions
return expression
will return the value ofexpression
to where the function was called from.- Function calls can be used in assignment statements to store return values, or as part of other expressions.