Function in Python refers to the block of statements that are intended to perform a specific task. To be specific, there are many Python functions that are used to perform a particular task.
For instance, print() is a function used to display the information on the output screen. Similarly, we have input(), for taking the user input. Such functions are known as built-in functions. In the same way, Python also provides the flexibility to created our own functions. These are known as user-defined functions.
Why we use functions in Python ?
1. Function allows us to follow OOPs principle like Modularity. Module represents a part of the program. Usually, it is suggested to divide the problem statement into various subtasks. Modular Programming makes thing easy.
2. Function refers to the block of code to perform specific tasks.
3. Once written, functions can be reused whenever required.
4. Code Maintenance and debugging is easy while working with functions.
5. Adding functions to the code will reduce the length of the program.
Defining a function
Python allows us to define the function using def keyword. Let us see how function definition works :
def function_name(para1, para2, ...):
function statements
As mentioned, we use def keyword to define a function along with function name and parameters(if any).
Consider following definition :
def multiply(num1, num2):
return num1 * num2
def represents the initial point of function definition. Here, ‘multiply’ is the function name. After that, we need to add parenthesis ( ) mandatorily as interpreter will get know that this is function, not a variable or any other thing.
num1, num2 represents the variable, they are being used as function parameters. A function may or may not have function parameters.
After ( ), we add colon (:) that represents the function body. This function body is coined as suite which denotes the block of statements.
It is recommended to write string as the first statement in the function body. This string is referred to as docstring. It can have one or more lines. Below code specify the use of docstring :
def multiply(a, b) :
”’This function returns the product of a, b. Example of docstring”’
return None
After this, time is to write some cool code as a part of function body that defines the logic for a task. Make sure, your code should follow the correct indentation.
Indentation refers to the spaces at the beginning of a code line. Python uses indentation to indicate a block of code.
Calling a function
When we define a function, it does not have the ability to run on its own. It will only run once it is called. So, the next step is to call the function using its name. A defined function is called with its name and parameters.
Let’s call the function that we defined above :
multiply(10, 20)
Here, we are calling the multiply() function that we defined above to calculate the product of two numbers. In the above code, 10, 20 are arguments.
Now, let’s try to connect our complete code :
def multiply(num1, num2):
return num1 * num2
print(multiply(10, 20))
Output : 200
In the above code, we are calling multiply() function with two arguments 10 & 20. This function then returns the product of two numbers.
Storing values returned by a function to a variable
def multiply(num1, num2):
return num1 * num2
result = multiply(10, 20)
print(result)
Here, in the above code, we are storing the value returned by the function to variable result.
Points to Remember – Python Functions
1. In Python, it is possible to assign a function to a variable.
2. In Python, it is possible to have nested functions (function within function).
3. In Python, it is possible to pass a function as parameter to another function.
4. In Python, it is possible to return a function from one function.
That’s all in this lesson.