Defining Functions in Python
Function definitions in Python typically contain at least two lines. The first line defines the function name and arguments. The last line typically defines the function output. In between is the code that runs when the function is called.
def function_name(arguments): <code> return output
The first line of code above contains a couple of parts:
def needs to be the start of the line that declares the function.
def stands for definition and indicates to the Python interpreter that a function definition will follow.
Each function needs a name. The function name must start with a letter (or underscore) and is typically all lowercase (in Python, names that start with Uppercase are usually used to define classes). Function names can only contain letters, numbers and the underscore character. Just about any name will do, but it is best to avoid using any Python keywords such as
for. A complete list of reserved Python keywords is in the Appendix.
Function names are followed by a set of parenthesis
( ). Many functions have code, called arguments in between the parenthesis. The name used for the function argument(s) should be used in the body of the function. After the function name, parenthesis, and arguments comes a
: colon. In Python, a colon is required to end the first line of all functions.
The body of the function contains the code that will run when the function is called. Any variables declared by the function arguments can be used in the body of the function. Any variables used in the body of the function are local variables. Local variables cannot be called or accessed by other scripts, or used outside the function body.
return keyword is often the last line of a function.
return indicates that whatever expression that follows is the output of the function. The
return keyword is not a function or a method, and parenthesis are not used after
return, just a space.
Whatever expression is included after
return will be returned by the function. The output expression after
return can be a single variable, value or be a complex expression that includes multiple variables.
Your First User-defined Function
When you write your own functions, called user-defined functions, consider these criteria:
- What will you name the function?
- What, if any, input arguments will the function accept?
- What will the function do? What is the purpose of the code that runs when the function is called?
- What, if any, output will the function return?
Let's write a simple function which adds two to any number. We will call our function
plustwo. Our function has one input argument, a number. The function will return that number plus
Let's apply this description to our four criteria:
- Function name:
- Input arguments: a number
- What does the function do: add 2 to any number
- Output: a number (2 + the input number)
plustwo() function will operate as shown below:
The code section below defines our
def plustwo(n): out = n + 2 return out
def, a space and then the function name
plustwo. The input argument,
n, is enclosed in parenthesis
( )after the function name. After the set of parenthesis is a colon
:. The body of the function includes the code
out = n + 2. The last line of the function includes the keyword
returnfollowed by a space and the variable
out. Note variable
nis a local variable and can only be used inside the function definition.
Let's run our
plustwo() function and see the output.
plustwo()function can be assigned to variable.
ans = plustwo(10) ans