This article will focus on Python Functions. We will learn Python function-defining a function, calling a function, parameters and arguments, returning values in functions and Recursion.
Few quick words: I will suggest you to download the latest version of Python which will help you to practice coding python programs. In case you do not have Python installed in your system you can use this link to online run your Python codes.
I also suggest you to visit the article here to learn the basic concepts of Blockchain.
- A function is a named piece of code which runs only when it is called.
- First you define a function (we will see in the following section). Then you call the function.
- A function can take any number and type of input parameters and return any number and type of results.
Defining Python Functions
- To define a function, we use def followed by name of the function followed by parenthesis enclosing any parameters to the function, then finally a colon (:)
- Syntax: def function_name( ):
- Function names have the same rules as variable names. They can start with a letter or _ and contain only letters, numbers or _
- Here we have defined a function with no parameters. We used the pass statement in the next line. Python uses pass to show that the function defined has no parameters and hence does nothing.
Call a Python Function
- To call a function, we simply write the function name and parenthesis.
Syntax: function name( )
- For instance, to call the function function_example() in the previous example, we simply write as below
- Let us define another function with no parameters but which has a code inside its definition to print a single word.
- Here the function dog_sound( ) has no parameters. But it has the code – print(‘barks’) inside its definition. Hence when we will call the function dog_sound( ) it will print ‘barks’.
- Let us try another function ghosts( ) which returns the value True. The we call the function with if statement. See below.
Arguments and Parameters in Python Functions
- When defining a function, the value that comes in between the parenthesis of the function is called the parameter. Whereas, when calling a function, the value that comes in between the parenthesis is called the argument.
- When a function is called with an argument, the argument is copied into the corresponding parameter within the function. Let us see an example.
- In the above example, the function scream( ) is defined with parameter ‘echo’. The code inside will be executed when the function scream( ) is called. In the third line the function is called with argument ‘John’. Hence now ‘John’ will be copied or replace the parameter ‘echo’ inside the function.
- What will happen when we call the function scream( ) with argument ‘Narendra Modi’.
- Let us now combine conditional statements in Python functions.
Here we have defined the function thing( ) which has the parameter ‘color’. The code inside the function is executed when the function thing( ) is called.
Let us call the function with different arguments and see the outputs.
Multiple arguments and parameters
- We can call a function with one or more arguments of any type (including zero). When using multiple arguments we separate them with commas. Also the number of arguments you call a function with should be the same as the number of parameters the function was defined with.
- In the below example, the function couple( ) is defined with two parameters. Hence, when we call the function we use two arguments.
- Now if we try and call a function with lesser arguments than the parameters, we get error.
Arbitrary arguments with * ||When the number of arguments is unknown
- When we do not know the number of arguments with which the function will be called, we add a * before the parameter name in the function.
- The function can receive a tuple of arguments and can access the respective item.
Arbitrary arguments are often shortened to *args in Python documentation.
- Python handles arguments in functions in a manner that is very flexible as compared to other languages.
- One such example is the Positional Argument. In Positional Argument, out of the given number of arguments, each argument is copied to the respective parameter in the order in which the parameters are defined.
- One downside with Positional arguments is that we need to remember the position of each parameter and assign the arguments accordingly. For instance, if in the above example, we had written the argument “red” in the end, it would have messed up with the colors of the items. See below.
- To combat this issue Python comes up with Keyword Arguments discussed in the next section.
- To avoid the confusion of Positional arguments, we can specify each argument with the names of their respective parameters, even in a different order from their definition in the function.
- We can mix positional and keyword arguments as seen below.
- But when we mix positional and keyword arguments, the positional argument needs to come first.
Arbitrary keyword arguments with **
- When the number of keyword arguments are unknown, use double asterisks ** with the parameter. This way the parameter with ** will take the keyword arguments as a dictionary.
Default value for Parameters
- We can specify default values for parameters, which are used when the caller does not specify a corresponding argument.
- For instance see below
In this case the default value of the parameter ‘vodka’ is ‘Smirnoff’. Hence, whenever (while calling the function liquor) we do not define the corresponding argument of ‘vodka’, it will take the default parameter ‘Smirnoff’.
But when we specify an argument, it supersedes the default parameter. See below.
Arbitrary arguments with *
- There are some cases, where you do not know the number of arguments with which the function will be called.
- In these cases, use an asterisk * with the parameter, which will pass all the arguments as a Tuple to the parameter.
- You can see that the * binds the positional arguments into a Tuple in the parameter.
Arbitrary arguments are often shortened to *args in Python documentation.
Returning values with the ‘return’ statement
- Python functions can return a value using the return statement.
The respective outputs are:
- A Python function can call itself in its definition. This phenomena is called Recursion.
- For instance, lets see the below example.
- When we call the function fun( ) for the first time, it will assign value 10 to x and then calls itself recursively. Hence again the function (at a different space in computer memory), assigns value 10 to x and calls itself. Theoretically this process will go on forever. But in practice nothing is forever. Your computer has only so much of computing power. After a certain time the computing power will run out and the program will stop. However, Python doesn’t let your computer to run out. It sets a limit on the number of times the recursion can happen and after that sends a traceback. Let us see.
- The recursion limit of the Python interpreter is a large number. Besides, there is no use of calling a function recursively these number of times.
- Hence we define a base case or a condition, which when is met, the function stops.
- Let us try to set some condition in the recursion example above.
Now in the example above we have defined a function fun(x). When the fun(x) is called with an argument for x, first it checks whether the argument is greater than zero or not. If it is greater than zero, it prints x and then recursively calls itself with argument (x-1). Again the function checks whether (x-1) is greater than zero or not. If it is >0 it prints (x-1) and calls itself recursively with argument ((x-1)-1). This process goes on until x<=0. Here the else statement is executed and the recursion is terminated (return statement). Let’s check this by calling the functions with argument 10.
In this article we read about Python function-defining a function, calling a function, parameters and arguments, returning values in functions and Recursion. Functions are an important concept in Python and in other programming languages also. Next we will explore other concepts in Python programming language.