Python is a high level, interpreted programming language, designed and developed by Guido van Rossum in the 1990s. It is known for its simplicity and easy to understand capability. It is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of functions. Let us have a look at them in detail.
In python, a function, just like other programming languages, is a block of code that is usually meant to be used multiple times. The block of code usually contains related statements that are meant to perform a particular task or operation and generate some form of result. Python functions are of two types - built in or user defined. Instead of writing the same code multiple times in a repetitive manner, we can use functions to separate our program into different chunks or modules to make it organized. A function may or may not return a value.
The syntax of creating a function is very straightforward and is as follows. We simply use the def keyword. This is followed by the function name and the parameters enclosed within round brackets along with a semicolon. From the next line onwards we specify the statements to be executed when the function is called. Keep in mind the indentations that are required here.
def <function_name> (<parameters>):
To actually use a function, we need to call it. The syntax of calling a function is very straightforward and is as follows. We simply use the function name followed by required parameters enclosed within round brackets. If there are no parameters, we specify it using empty round brackets. The terms parameter and argument can be used interchangeably.
This is a very straightforward example. Using the syntax specified earlier, we define a function named func which takes in two parameters, a and b, and returns the sum of these parameters after assigning it to another variable named sum. One thing to note here is that we did not call the function, just defined it. So we will not receive any output. Let us look at the python code and corresponding output for this example -
# Defining the function def func(a, b): sum = a+b return sum
Let us now take a look at how we should call this function to produce the required output. We call the function func using the function name followed by parameters enclosed between round brackets, here 2 and 3. We print the value returned by the function, here 5. Let us look at the python code and corresponding output for this example -
# Defining the function def func(a, b): sum = a+b return sum # Printing print(func(2,3))
It is not necessary to have a return statement in the body of a function. Here we look at almost the same function as before that calculates the sum of two given parameters. The difference is that we print the sum directly instead of returning it. We call the function with parameters 2 and 3 and the value 5 is printed there. Let us look at the python code and corresponding output for this example -
# Defining the function def func(a, b): print(a+b) # Calling func(2,3)
If we call a function without specifying the exact parameters that are required, we get an error instead of the desired output. Here we have a function func that takes in two arguments a and b and prints the sum of these arguments. We try to call this function using only 1 argument instead of the required 2 and get an error as mentioned below. Let us look at the python code and corresponding output for this example -
# Defining the function def func(a, b): print(a+b) # Calling func(2)
TypeError: func() missing 1 required positional argument: 'b'
In python variables have two types of scopes with respect to a function - local and global - depending on where they are defined. In in the example below, a is a local variable defined and used within the body of the function whereas b is a global variable defined outside the function but used within it. Let us look at the python code and corresponding output for this example -
def func(): a = 1 print("Local variable = ", a) print("Global variable = ", b) b = 2 func()
Local variable = 1
Global variable = 2
In this topic, we have learned the use and advantages of functions in a Python program, following a few running examples, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to [email protected] in case of any suggestions