Python function

Python function is defined using def keyword. A function may contain no of statement that execute when function is activated. A function activation means function call. A function is always called by its name. Function name also must be a valid identifier. If we want to define a function that have no one statement then we have to write pass keyword in body of the function. Otherwise it will raise an error.

Similar to other high level programming languages python function has also the following properties.

  • Function name / identifier
  • Parameters / Arguments
  • Python return
Function Name Function name is the identifier that is used to define and call the function. This identifier also fulfill the rule of naming identifiers in python.
Parameters Function parameters are the value or data provided to the function at the time of function call. A function can accept multiple parameters. If function call does not provide required no of arguments then it will generate error. Parameters are optional in function defination.
Python return Function return is normally known as output of the function. It is optional feature that can be performed using function. In python a function can also return a list of values. Return keyword is used for returning data from function. In general it is the last statement of the function.

Deifining Function:

In python programming def keyword is used for defining a function. We can define multiple functions accepting similar or different no of parameters.


def  function_identifier ( parameter list ) :

     " " "  docstring " " "

     function body

Where function_identifier is the name of function and parameter list is parameters separated by comma. Function body can also contain return statement. The docstring is used to write function description and can easily accessed using function identifier and __doc__ standard attribute.

Python docstring:

Python has feature to write detail description or functionality of functions. Docstring is written after function defination line (function header). This description can be accessed using __doc__ to know about use / description of function.

Function Call:

A statement that transfer the execution control to the function defination is refer as function call. Function is always called using function identifier. In python when function is call first of all the parameter list is matched. If it does not match then it will raise an error. We can call function multiple times as per requirement.

Python function defination and use of local, global variables

Types of functions:

Similar to other high level programming languages python also has two types of functions.

Standard Functions:

All the functions that are part of some library and can be used after importing this library are called standard or bult in functions. These functions makes development much fast and easy.Python has number of libraries with a lot of standard functions. Some standard functions are.

abs() It will return absolute value
bin() Return binary string of integer.
chr() It will convert integer into string.
dict() It is used to create python dictionary.
len() Returns the length of object.
min() It will return the smallest value.

User Defined Function

A function which is defined by the programmer in python script file is known as user defined function. These functions are created according to requirement. User defined function are used to devide the complicated logic of program in smaller undestandable modules.

Python sqrt

Python square root is built-in function that is part math library.

sqrt Example:

# import math library
import math

# defining function
def func(a):  # accepting single parameter
    return math.sqrt(a) # return statement

# function call



Python Arbitrary Arguments:

Python programming also provide the feature to define a function that can accept a list or array of arguments. To define a function that can accept list of arguments we have to use asterisk (*) symbol before the function parameter name.


# function defination
def fun(*prm):  # accepting parameter list
    """ Hello Function """
    print(prm[1]) # accessing second argument

# function call statement



Python Local and Global variables:

All variables that are declared inside scope of control strucures or function defination are called local variables. These variables are only accessible inside the function where these are declared. On the other hand all variables that are declared ouside the scope or body of functions are called global variables. These variables can be accessed anywhere in the program.

Global variable example:

G = 10

# function defination
def fun():  # accepting no parameter
    Use of Local and
    Global Variables

    G = 12  # declaring Local variable G
    L = 15  # declaring Local variable L
    print(G) # will print local variable

# function call statement



In this function a local variable L is also declared. If we try to access this variable outside of function then it will generate an error.

Default Argument Value:

In python we can also set some default value for the function parameter. This parameter is also called optional parm. When value is passed then the passed value will be used in function body, otherwise default value wil be used. Default argument is assigned using assignment operator.


# function defination
def fun_def(parm = "Default"):  # accepting one parameter
    Use of default parm value

    print(parm) # will print parm value

# function call statement


Login to TRACK of Comments.