Functions
What is a Function?
Function is a set of Python codes that performs a task or returns a result.
So far, you have learnt how to use some of the builtin function in Python, such as print()
, len()
, range()
, min()
, max()
etc.
You can write lines of codes to perform a task or return results. But the lines of code gets larger from few lines to 100s or 1000s of lines of code. It is recommended to be break down the code to smaller chunks. This is helpful to make your code more maintainable and readable as well as avoid repetition by reusing chunks of codes. These reusable chunks are called Functions.
What are the Types of Functions?
There are generally two types of functions:
Perform a task
Return a value(s) or an expression(s)
What is a Function Syntax?
In Python, the building block of a function is as follow:
Function starts with
def
keywordA uniquely identified name for the function
Parameters to pass the value(s) of the function into the function. This is optional.
A way to define where the function header finishes:
colon :
Multi-line string to document what the function input and output is or performs. This is also optional but it is recommended.
Python statement(s) that performs a task
If you want the function to return an output, you can use
return
statement. It is also optional.
What is return
Statement?
return
Statement?The return
statement is used to exit a function and return an output. The output can be assigned to a variable with assignment operator. The return
statement is:
The return
statement evaluates the function and returns the output. It will return a None
object where there is either no expression to return
or no return statement is present.
An Example of a Function
An example of a function that takes two integers and return the sum of the values. The function is created below:
Semantics: Arguments vs Parameters
In the context of take_sum()
function, value_1_int
and value_2_int
are called parameters of take_sum()
function. value_1_int
and value_2_int
are variables locally connected to the take_sum()
function.
You can call the function name followed by a parentheses () with the arguments in between.
Now, value_1_int
and value_2_int
are called the arguments of take_sum()
and you also passed value_1_int
and value_2_int
by reference. i.e. memory addresses of the value_1_int
and value_2_int
are passed to the function.
A function can be created without parameters.
What is Positional Arguments?
Positional Arguments is the order in which parameters are passed into a function. It is the most common way of assigning arguments to parameters.
What is a Default Values in a Function?
A positional arguments can be made optional by specifying a default value for the corresponding parameter. Let's take the take_sum()
function as an example.
The default value always comes as a last parameter in the function or any parameter that comes after it has to be assigned with default value. Let's modify take_sum() function to have three parameters.
But what if you want to specify the 1st and 3rd arguments, but omit the 2nd argument?
i.e. you want to specify values for value_1_int
and value_3_int
, but let value_2_int
take on its default value.
Keyword arguments
Optionally, positional arguments can be specified by using the parameter name regardless of the parameters with or without default values.
When you use a named argument, it is required to use named arguments for all arguments after it.
You can also not call the named arguments when you are calling your function
What is *args
?
*args
?Remember the iterable unpacking
Similar things happens when positional arugments are passed to a function
There is a handy trick that can be used during tuple unpacking when you have imbalance on right and left of the assignment operator. You can use a * operator to create a tuple/list from several values.
The above code gives an error. But we can overcome that with * operator. The first and second will be assigned to x and y respectively. The third and fourth will be a list and assigned to z
Let's define the function again, notice the start at the start of third value.
The parameter name value_3_int
is arbitrary. You can choose any name you would like, but it is customary to name it *args
. So your function can be updated as below.
*args
will not allow anymore positional arguments. But there is a way around it.
This will not work
This will work
Unpacking arguments
You can pass an iterable argument into a function with the * operator
.
What is Keyword Arguments?
The positional parameters can, optionally, be passed as named (keyword) arguments. For example, if we define a function with three positional arguments:
We can call the function get_sum()
and pass the arguments. The named arguments is optional and it is up to you.
You can also rewrite the function above to make more intuitive and practical. Using *args
, you can pass many values at the same time.
Mandatory Keyword Arguments
Sometimes, you need to make the keyword arguments mandatory and force the user to call the named arguments. You can do so by exhausting all the positional arguments, and specify another additional parameter or parameters in the function definition.
The presence of *args
exhausts all positional arguments and int_3
MUST be passed as a named argument.
In the function get_sum()
, you will need to pass two positional arguments, one or more additional arguments (optional), and a mandatory keyword argument which goes into int_3
. The int_3
argument can only be passed to the function using a named (keyword) argument.
This will not work
But using named argument for int_3
, it works.
You can even define a function that has only optional positional arguments and mandatory keyword arguments, i.e. you can force no positional arguments at all.
The function get_sum()
can be rewritten to provide no positional arguments at all.
Now, you have only * without specifying the name (usually args). The * indicates the end of positional arguments. You tell your function that you are not looking for positional arguments, effectively, there is no positional arguments.
if you call the function and provide positional arguments, Python raises an Exception error.
Note: The aims is to showcase no positional arguments and the get_sum()
function has to be fixed.
In nutshell: *args vs *
x: mandatory positional argument (may be specified using a named argument)
y: optional positional argument (may be specified positionally, as a named argument, or not at all), defaults to 1
args vs *
args: catch-all for any (optional) additional positional arguments
*: no additional positional arguments allowed
z: mandatory keyword argument
factor: optional keyword argument, defaults to True
What is **kwargs?
You have a clear understanding what*args
is by now. **kwargs
works very similar *args
, but instead of accepting positional arguments (*args
returns a tuple) and accepts named arguments.
**kwargs
is used to accept a variable amount of remaining keyword arguments. It returns a dictionary
.
**kwargs
can be specified even if the positional arguments have NOT been exhausted. This is opposite to *args
.
Let's define a function:
You can also use it by passing only the named argument int_1
This will print only the mandatory positional argument and an empty dictionary.
You can also create a function by just passing a **kwargs
. For example:
You can even create a function with both *args
and **kwargs
.
Can we mix args and kwargs as input to a function?
You can even call the function without providing any arguments. This result in returning an empty tuple ()
and an empty dictionary {}
.
You can check the Jupyter Notebook in Colab
Last updated