Python Functions
Functions in Python are predefined blocks of code that are required to do a specific task. Functions make your code smaller and provide flexibility to it and thus your code can be reused multiple times.
There are predefined function in Python like print(), etc., but you can also write your own functions.
How to Define a Function
A function can be defined in a simple way like this-
Example
- We give our function a name by using the keyword def. This is called function definition.
- Then, the only task of the function is described that is to print the string “Hi! How are you?”
- In this line, we call the function by writing the function’s name followed by closed small brackets.
Docstrings
A docstring explains what the function does. It is enclosed in triple quotes and Python searches for it in a function while making documentation for the function.
You can read the docstring by using __doc__ keyword.
Please note that in ‘__doc__’, there are two underscores on either side of the word ‘doc’.
Example
Passing Information
We can change the function start_a_conversation() a little bit, so that it not only asks the user “Hi! How are you?” but also uses their name to ask the question.
This can be done by writing some information in the brackets that follow the function’s name, the information is called a parameter. Let’s see how
Example
The value ‘Roger’ is called an argument.
Now, as long as the task inside the function can be performed, Python won’t give an error to check the data type of the argument passed to the function, in this case, the argument should be of data type ‘str’.
Passing List as an Argument
Let’s have a better example for Python Functions where the argument will be a List. We are going to create a function that cubes and adds all of the items in a list using a for loop –
Example
Default Values
While writing parameters for a function, you can assign a default value for one or more parameters. However, the default values can be altered while passing arguments.
Example
- The function used the default value ‘Potter’ as the last name of the person.
- The function used the most recent argument as the last name that is ‘Trask’.
Importance of Order while Passing Arguments
If a function takes more than 1 parameter, then while passing the arguments, you should be aware of the order in which you pass them.
Example
As you can see in the output that due to the order of the arguments the username got mixed up with the name of the book.
How to avoid committing this mistake?
In Python, while passing arguments, you can specify which argument belongs to which parameter like this: –
Example
These kinds of name-value pairs are called keyword arguments.
If a function expects 2 arguments and only 1 argument is passed, the compiler will give an error.
Example
Arbitrary Arguments
If the number of arguments to be passed is unknown to you, then by adding an asterisk sign * behind the parameter can solve the problem that came in the example above.
By doing this, Python converts the arguments into a Tuple and then uses them.
Example
The arguments are converted into a Tuple so we can also perform all kinds of operations inside the function that we are able to perform on a Tuple.
Example
Arbitrary Keyword Arguments
If the number of keyword arguments to be passed is unknown to you, then add two asterisks ** before the parameter name.
By doing this, Python will convert the parameters into a dictionary and items can be accessed according to the need.
Example
Sometimes, people either don’t have a middle name or don’t want to provide it, so with the help of arbitrary keyword arguments, we can make them optional.
These are also known as **kwargs.
Returning Values
We do not have to use print all of the time to display a simple output. Instead, the function can return a set of values to the user. The values returned by a function are called return Values.
Example
Returning a Dictionary
Example
As you can see above that a Dictionary was returned by the function. Likewise, any kind of value like a List, Tuple, etc., can be returned using the return keyword.