python

Functions in Python


  • If a group of statements is repeatedly required then it is not recommended to write these statements every time separately. We have to define these statements as a single unit and we can call that unit any number of times based on our requirement without rewriting. This unit is nothing but function.
  • The main advantage of functions is code Reusability.

 

Types of Function

 

❑ Built-in Function:

The functions which are coming along with Python software automatically are called built in functions or pre defined functions. 
Eg:- id(), print(), type(), input(), eval() etc.

 

❑ User defined Function:

The functions which are developed by programmer explicitly according to business requirements, are called user defined functions.

syntax:

def function_name(parameters) :
    """ doc string"""
    statementN
    return value

Note:- Where def is mandatory and return is optional.

parameter:

  • Parameters are inputs to the function. If a function contains parameters, then at the time of calling, compulsory we should provide values otherwise, otherwise we will get error.


return:

  • Function can take input values as parameters and executes business logic, and returns output to the caller with return statement. We can return a single object at a time.
  • If we are not writing return statement then default return value is None.
Note:
• A parameter is the variable listed inside the parentheses in the function definition.
• An argument is the value that are sent to the function when it is called.

 

Types of Arguments

 

❑ Positional Arguments:

  • Order should be maintained, otherwise result will be changed.
  • Total number of argument should be matched, else we will get error.

❑ Keyword Arguments:

  • Orders are not important.
  • Total number of argument should be matched, else we will get error.
  • We can use both positional and keyword arguments simultaneously. But first we have to take positional arguments and then keyword arguments, otherwise we will get syntax error.

❑ Default Arguments:

  •  If we are not passing any name then only default value will be considered.
  • After default arguments we should not take non default arguments else we will get syntax error.

❑ Variable Length Arguments:

  • Sometimes we can pass variable number of arguments to our function, such type of arguments are called variable length arguments.
  • We can declare a variable length argument with symbol.
  • We can call this function by passing any number of arguments including zero number. Internally all these values represented in the form of tuple.
Note:-
• We can mix variable length arguments with positional arguments.
• After variable length argument, if we are taking any other arguments then we should provide values as keyword arguments.
• We can declare key word variable length arguments also. For this we have to use **. We can call this function by passing any number of keyword arguments. Internally these keyword arguments will be stored inside a dictionary.

 

Types of Variable

 

❑ Global Variable:

  • The variables which are declared outside of function are called global variables.
  • These variables can be accessed in all functions of that module.

❑ Local Variable:

  • The variables which are declared inside a function are called local variables.
  • Local variables are available only for the function in which we declared it.

 

Global keyword:

  • To declare global variable inside function
  • To make global variable available to the function so that we can perform required modifications
Note:-
If global variable and local variable having the same name then we can access global variable inside a function as follows:
Syntax: (globals()['variable_name']

 

Recursive Function:

A function that calls itself is known as Recursive Function.

The main advantages of recursive functions are:

  • We can reduce length of the code and improves readability
  • We can solve complex problems very easily.

 

Anonymous Function:

Function without any name called anonymous functions or lambda functions. 
The main purpose of anonymous function is just for instant use. It can be define by using lambda keyword.

Syntax:- 

lambda argument_list : expression

Lambda Function internally returns expression value and we are not required to write return statement explicitly.

 

Function Aliasing

For the existing function we can give another name, which is nothing but function aliasing.

Syntax : 

variable_name = function_name

 

Nested Function

Function within another function called nested function.

Note:
• In python, everything is object.
• A function can return any object, even function also because function is also an object.

 

Decorator

  • Decorator is a function which can take a function as argument and extend its functionality and returns modified function with extended functionality.
  • The main objective of decorator functions is we can extend the functionality of existing functions without modifies that function.
  • Prefix @decorator_name with function_name

 

Generator

  • Generator is a function which is responsible to generate a sequence of values.
  • We can write generator functions just like ordinary functions, but it uses yield keyword to return values

Advantages of Generator

  1. when compared with class level iterators, generators are very easy to use
  2.  Improves memory utilization and performance.
  3. Generators are best suitable for reading data from large number of large files
  4. Generators work great for web scraping and crawling.

 


python