January 18th 2020 846
A piece of source code, separate from the larger program, that performs a specific task.
Reasons to use functions:
- Reduce complex tasks into simpler tasks
- Eliminate duplicate code
- Code reuse
- Distribute tasks to multiple programmers
- Hide implementation details - abstraction
- Improves debugging by improving traceability
Functions are defined in Python using
def functionname(parameters): """function_docstring""" # function_statements return [expression]
To let a function return a value, use the
function definitions cannot be empty, but if you for some reason have a
function definition with no content, put in the
pass statement to avoid getting an error.
def func(): pass func()
Any variable, values, or any data types (list, string, set, dictionary) can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.
Arguments are often shortened to
argsin Python documentations.
Parameters or Arguments?
The terms parameter and argument can be used for the same thing: information that are passed into a function.
From a function's perspective:
- A parameter is the variable listed inside the parentheses in the function definition.
- An argument is the value that are sent to the function when it is called.
Number of Arguments
By default, a function must be called with the correct number of arguments. Meaning that if your function expects 2 arguments, you have to call the function with 2 arguments, not more, and not less.
Example: This function expects 2 arguments, and gets 2 arguments
If you try to call the function with 1 or 3 arguments, you will get an error:
Example: This function expects 2 arguments, but gets only 1
Arbitrary Arguments, *args
If you do not know how many arguments that will be passed into your function, add a
* before the parameter name in the function definition.
This is known as
This way the function will receive a tuple of arguments, and can access the items accordingly:
Example: If the number of arguments is unknown, add a
* before the parameter name
Arbitrary Arguments are often shortened to
*argsin Python documentations.
You can also send arguments with the
key = value syntax.
This way the order of the arguments does not matter.
The phrase Keyword Arguments are often shortened to
kwargsin Python documentations.
Arbitrary Keyword Arguments, **kwargs
If you do not know how many keyword arguments that will be passed into your function, add two asterix:
** before the parameter name in the function definition.
This way the function will receive a dictionary of arguments, and can access the items accordingly:
Example: If the number of keyword arguments is unknown, add a double
** before the parameter name
Arbitrary Kword Arguments are often shortened to
**kwargsin Python documentations.
Default Parameter Value
The following example shows how to use a default parameter value.
If we call the function without argument, it uses the default value:
Python also accepts function
recursion, which means a defined function can call itself.
Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.
Example: Factorial of a number
function which is defined inside another function is known as nested function.