FREE E LEARNING PLATFORM
HOMEEXCEPTIONSOOPSJVMINTRO
 

Python Functions




In this guide, we will learn about functions in Python. A function is a block of code that contains one or more Python statements and used for performing a specific task.

Why use function in Python?

As I mentioned above, a function is a block of code that performs a specific task. Lets discuss what we can achieve in Python by using functions in our code:

1. Code re-usability: Lets say we are writing an application in Python where we need to perform a specific task in several places of our code, assume that we need to write 10 lines of code to do that specific task. It would be better to write those 10 lines of code in a function and just call the function wherever needed, because writing those 10 lines every time you perform that task is tedious, it would make your code lengthy, less-readable and increase the chances of human errors.

2. Improves Readability: By using functions for frequent tasks you make your code structured and readable. It would be easier for anyone to look at the code and be able to understand the flow and purpose of the code.

3. Avoid redundancy: When you no longer repeat the same lines of code throughout the code and use functions in places of those, you actually avoiding the redundancy that you may have created by not using functions.

Syntax of functions in Python

Function declaration:

def function_name(function_parameters):
function_body # Set of Python statements
return # optional return statement

Calling the function:

# when function doesn't return anything
function_name(parameters)

OR

# when function returns something
# variable is to store the returned value
variable = function_name(parameters)

Python Function example

Here we have a function add() that adds two numbers passed to it as parameters. Later after function declaration we are calling the function twice in our program to perform the addition.

def add(num1, num2):
    return num1 + num2


sum1 = add(100, 200)
sum2 = add(8, 9)
print(sum1)
print(sum2)

Output

300
17

Default arguments in Function

Now that we know how to declare and call a function, lets see how can we use the default arguments. By using default arguments we can avoid the errors that may arise while calling a function without passing all the parameters. Lets take an example to understand this:

In this example we have provided the default argument for the second parameter, this default argument would be used when we do not provide the second parameter while calling this function.

# default argument for second parameter
def add(num1, num2=1):
    return num1 + num2


sum1 = add(100, 200)
sum2 = add(8)  # used default argument for second param
sum3 = add(100)  # used default argument for second param
print(sum1)
print(sum2)
print(sum3)

Output

300
9
101

Types of functions

There are two types of functions in Python:

1. Built-in functions: These functions are predefined in Python and we need not to declare these functions before calling them. We can freely invoke them as and when needed.

2. User defined functions: The functions which we create in our code are user-defined functions. The add() function that we have created in above examples is a user-defined function.







Leave Comment