top of page

Functions

 

Functions are reusable pieces of programs. They allow you to give a name to a block of statements, allowing you to run that block using the specified name anywhere in your program and any number of times. This is known as calling the function. We have already used many built-in functions such as len and range.

Defining a Function


Rules to define a function in Python.

  • Function blocks begin with the keyword def followed by the function name and parentheses( ).

  • Any input parameters or arguments should be placed within parentheses. 

  • You can also define parameters inside these parentheses.

  • The first statement of a function can be an optional statement - the documentation string of the function or docstring.

  • The code block within every function starts with a colon (:) and is indented.

  • The statement return [expression] exits a function, optionally passing back an expression to the caller.

 

Syntax


def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]
   

Invoking a Function:


<function_name>(parameters)
   
# Print function with docstring   
def hello ():
    "Just print hello"
    print "hello"
hello ()

# Print function without docstring      
def hello ():
    print "hello"  
hello ()

return Statement:

return[expression] is used to send back the control to the caller with the expression.

def sum(a,b):  
            "Adding the two values"  
            print "Printing within Function"  
            return a+b 
print sum (1,2)

Function Arguments


You can call a function by using the following types of arguments:

  • Required arguments

  • Keyword arguments

  • Default arguments

  • Variable-length argument
        

# required arguments

def add (x,y):

    print x+y
add (1,2)

#keyword arguments

def add (x,y):
    print x+y
add (x=1,y=2)

#default arguments

def add (x=1,y=1):
    print x+y
add (1,2)
add ()

 

#Var length arguments

def add (*arg1):
    print arg1
add(1,2,3)

def add (*arg1):
    print arg1
    for var in arg1:
      print var+1
add(1,2,3)

Recursive function

   

A recursive function is a function which either calls itself or is in a potential cycle of function calls. As the definition specifies, there are two types of recursive functions. Consider a function which calls itself: we call this type of recursion immediate recursion.


# find the factorial of a number

def fac(x):
    """This is a recursive function
    to find the factorial of an integer"""

    if x == 1:
        return 1
    else:
        return (x * fac(x-1))

num = 4
print"The factorial of", num, "is", fac(num)        

 

Anonymous function


In Python, anonymous function is a function that is defined without a name.

Syntax of Lambda Function
 

lambda arguments: expression

Lambda functions can have any number of arguments but only one expression. The expression is evaluated and returned. Lambda functions can be used wherever function objects are required.

# Program to show the use of lambda functions
double = lambda x: x * 2
print(double(5))

© 2015 Thirumurugan

bottom of page