-
Classes
-
File operations
-
Comprehensions
-
loops
-
Python Installation
-
Numbers
-
Data Types in Python
-
Operators
-
Sequence Data types part 3 – Tuples
-
Sequence Data types part 2 – Lists
-
Expressions and statements
-
Data Unpacking
-
Functions
-
Dictionaries
-
Sequence Data types part 1-strings
-
if…else
-
Taking Input from the user
-
Python Typecasting
A function is a reusable block of code that can be called to execute a particular functionality.
The def keyword is used to define a function in Python.
Let us see a simple function definition:
When we run this code, we get 11 in the output.
Here def is the keyword used to define a function in Python. addNums is the name of the function. The function name is followed by a pair of parentheses which is used to define parameters which we will see after some time. The colon(:) indicates the start of the function block. The statements inside the function block start with an indentation. Without the indentation, Python will throw an error just like if, else, while and for statements. Whatever operations we want to do should be placed inside the function block. Now, what we have created till now is known as the function definition. This function will never be executed until we tell Python to execute it. It will execute when we “call” it. As we can see in the above example, we are calling the function below after defining it. To call the function, we need to use the name of the function followed by a pair of parentheses. The parentheses tells Python to call the function.
Now, let us see another example. The above function will always print “hello” whenever we call it. But let’s say we want to tell the function to print a different value every time I call it.That is not possible with the existing function. Now is the time to introduce parameters that I mentioned earlier. We can specify something called parameters while defining the function. This tells Python that the function can accept user-assigned values and give the respective output.
Let us see a relevant example:
Output:
Hello
Hi
Welcome
Here, we can see that we can pass a string to the function while calling it. The strings that we are passing(“Hello”, “Hi” and “Welcome”) are known as arguments. Note that in the function definition, there is a text value within the parentheses. This text value will receive the argument passed(“Hello”, “Hi” and “Welcome”) to the function call. This text value is known as a function parameter. A function can have n number of parameters to which arguments can be passed while calling the function. The function receives the argument, passes it to the parameter and performs the necessary actions on the parameter. In the above example, the function receives the string argument, passes it to the text parameter and prints the value. So, we are able to pass different strings to the function and print them as and when required. Let us see one more example.
Output:
5
Here, the function accepts two parameters and prints the sum of the inputs. We are passing 2 and 3 as arguments to the function call. The parameters x and y are receiving the 2 and 3 respectively. Eventually, the sum of 2 and 3 which is 5 will be printed in the output.
It is not always necessary for a function to print the output. We may want to reuse the calculated value somewhere else. For example,
Here, instead of printing the sum of the two values, we are storing the sum in a variable. Unfortunately, the sumValue variable is a local variable for the function and cannot be accessed outside the function. In this case, we need to return the value from the function.
A function can return or give us back some value with the help of the return statement. But the above example doesn’t have any return statement. Even this function returns some value.
First, let us see how to retrieve the return value from a function.
Here, we are assigning the function call to a variable. This variable will hold the return value of the function. In the above example, printing the returnValue gives None in the output. None is a data type used to represent the absence of a value. So, a function returns None if there is no return statement inside the function. Now, let us reframe the above function to return the sumValue as follows:
Here, we have a return statement inside the function which is returning the sumValue. So, on printing returnValue, we get 5 in the output.
Till now, we have seen only one way to define parameters and pass arguments to a function. Let us see what other ways we can do this in the following section.
Default arguments
Let us take the previous function once again:
Here, in the function definition, there are two parameters. But while calling, we are passing only one argument. If we run this code, we get the following error:
returnValue = addNums(2)
^^^^^^^^^^
TypeError: addNums() missing 1 required positional argument: ‘y’
The function call fails as it didn’t receive the second argument and it doesn’t know what value it should consider for the parameter ‘y’. In such cases, we can specify a default value for the argument to be considered if the argument(or arguments) is not passed. We can do it as follows:
Here, y is assigned a default value of 0. So when we don’t pass the second argument while calling the function, the function considers the value of y to be 0. So, it doesn’t throw an error in this case. The below mentioned function calls are all valid function calls.