Learn About Functions In Python

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.

  • A function can return a value, by using the statement: return 
    def return_example(a,b):    
        return a+b     
    x=int(input("Enter first number:"))    
    y=int(input("Enter second number:"))    
    print(return_example(x,y))    

    SAMPLE OUTPUT

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

  • From the beginning, we are using print() statement for output, for iteration using for loops we are using range() function, we used the constructor for making tuple; i.e. tuple(), for lists- list().
  • All these are functions whose functionality is pre-defined in Python.
  • The python interpreter has several functions that are always present for use.
  • These are known as Built-in functions.

User defined

  • The functions defined by us or by users to do a specific task are known as User-defined functions.
  • By using the keyword, def, we can make our own functions.
  • User-defined functions help to break down a large program into small segments which makes programs easy to understand, maintain and debug.

Recursion

  • When a function calls itself it is known as recursion.
  • In layman's terms, if we place two parallel mirrors facing each other, any object in between them would be reflected recursively.
  • It can be used for problems that have similar sub-problems; for example, sorting, searching, and traversal problems.

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

  • To understand Lambda Functions, first of all, we need to understand what Anonymous Functions are.
  • Anonymous stands for having no outstanding or unusual features. Anonymous functions can be used for containing functionality that need not be named and possibly for short-term use.
  • So, Lambda is Anonymous Functions that do not require a specific name.
  • It returns only one expression, so it does not require a “return” statement but can take multiple arguments.
  • We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all.
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.


Similar Articles