A function in Python is a block of code that is used to perform a specific task. Functions are basically used to avoid repetition or duplication in our code and make it much cleaner.
Let’s understand it. Suppose you want to print numbers from 1 to 10. So you will simply use a for loop to print numbers. Now let’s say that you need to print these numbers, again and again, maybe 10 or 20 times. What will you do in that case? You will simply write the same code 10 or 20 times. But isn’t that a bad idea, you are repeating the same code again and again. Functions help us to minimize this repetition.
So instead of repeating the same code, again and again, we can put that code inside a function and we can use it whenever or wherever we need it.
Creating a Function
A function in Python is created using the def keyword.
Syntax of a function
def function_name(): function body
The def keyword is an inbuilt keyword to create a function in Python. The function_name is any valid function name. After the function name, you have to put parentheses along with a colon. Which is responsible for indentation.
The function body should be indented inside the function. Any part of the code that is not inside the indented block will not be considered as a part of the function.
Note: The naming convention for a function is same as the naming a variable in Python.
Example of a Python Function
In this example we will create a simple function which prints a simple message.
# Creating a simple function named my_printer def my_printer(): print('Hello from my printer')
We have just created a function. But if you run above code it does nothing. Why??
It’s because we have only created the function but we are not using it anywhere. To use a function we have to call it where it is needed.
Calling a Function
To call a function, you have to simply write the function name along with parentheses (). We will use the same function my_printer which we created in the last example. Refer below explanation.
# creating and calling a function in Python def my_printer(): print('Hello from my printer') # calling above function my_printer()
Hello from my printer
# A function to add two numbers # create function def add(): num1 = 10 num2 = 20 print('Sum of', num1, 'and', num2, '=',num1+num2) # call add() function add()
Sum of 10 and 20 = 30
Passing Parameters to a Function
A parameter is simply some additional information that we pass to a function while calling it. A parameter can be of any valid or user-defined data type. Let’s understand it by an example:
# Passing parameters to a function in Python def add(num1, num2): sum = num1 + num2 print('Sum of ',num1,'and',num2, ' = ',sum) add(1,2) add(10,20)
Sum of 1 and 2 = 3 Sum of 10 and 20 = 30
In the above example, we passed two parameters while calling the add() function. In the first call, the variable num1 holds value 1 and num2 holds value 2 and prints the sum as 3. In the second call, the variable num1 holds a value 10 and variable num2 holds a value 20 and prints the sum as 30.
Note: If you don’t pass required parameters to the function while calling, it will throw an error. Refer below example:
# A program to demonstrate required parameters in Python def add(num1, num2): sum = num1 + num2 print('Sum of ',num1,'and',num2, ' = ',sum) # Pass only one parameter instead of two add(1)
Traceback (most recent call last): File "jdoodle.py", line 8, in <module> add(1) TypeError: add() missing 1 required positional argument: 'num2'
In the above example, we see that if we don’t pass all the required parameters to the function when we call, it will throw an error. But what if, you want to pass only one parameter to the function and the second value you want as a default value, maybe 10 or 20 or whatever. That’s what default parameters do.
A default parameter uses the default value if the parameter is not passed. Otherwise, it will use the value that is passed to this parameter. Refer below example:
# A program to demonstrate default parameters in Python def add(num1, num2=10): sum = num1 + num2 print('Sum of ',num1,'and',num2,' = ',sum) # passing only one parameter add(20) # Case 1 # passing both parameters add(20,30) # Case 2
Sum of 20 and 10 = 30 Sum of 20 and 30 = 50
In the example above, num2 is a default parameter.
In case 1, we only passed one parameter. Therefore, variable num2 used value 10 as a default value of it. Hence sum was 30.
In case 2, we passed both the parameters. Therefore, variable num2 used the passed value 30 instead of using the default value 10. Hence sum was 50.
Note: A default parameter uses it’s default value until we pass an external value to it.