Introduction

Functions are helpful to use the entire code and divide the code into useful blocks to do some computations and produce output.

Functions allow us to make a particular code more readable and reusable.

You can pass data, known as parameters, into a function.

When we define the body of a function, it is called Function Definition. When we declare this function/method in our code so that further in this code this function is being used, this is known as Function Declaration. When we want to use this function/method to be implemented we call it Function Calling.

From the above example, we know that a function is defined by using keyword- def()

When we want to pass something in the function, it is called an Argument or Parameters.

We can pass multiple arguments/parameters in a function, but that should be mentioned in function definition, else it will throw an error.

NOTE

Arguments are passed neither by value nor by reference in Python - instead, they are passed by assignment.

TYPES OF ARGUMENTS IN PYTHON

DEFAULT

An argument can have a default value and this is done by using (=), assignment operator. In case, we do not pass any argument then the default value is returned.

KEYWORD

Sometimes we are not aware of the order in which arguments were passed, so Python helps us with keyword argument by which we can pass arguments in any order.

ARBITRARY

Sometimes we are not aware of how many arguments we’ll get, so in this case, we use (*) before the argument. This helps us to return multiple arguments without knowing their count.

There are 4 different types of functions supported by Python,

Built-In

User defined

Recursion

Here is an example to show how recursion works by calling the function repeatedly

def recursion(demo):     
    if (demo < 1):     
        return    
    else:     
        for i in range(number):    
            print( demo,end = " ")     
            recursion(demo-1)    
            print( demo,end = " ")     
        return           
number=int(input("Enter how many you want repetition"))    
demo = int(input("Enter a Number"))    
recursion(demo)   

SAMPLE OUTPUT

Lambda

number1= int(input("Enter first number:"))    
number2= int(input("Enter second number:"))     
sum = lambda x,y: x+y    
print(sum(number1,number2))    

SAMPLE OUTPUT

SUMMARY

In this article, we discussed functions, how they can be used and their major parts. I hope this will help the readers to understand how to use and implement functions in Python.

Feedback or queries related to this article are most welcome.

Thanks for reading.