Functions
What is Module?
A Module is a file that contains functions, classes and
variables. Example – math, random etc.
Functions
✔ These are also known as Subprogram.
✔ These can be built in, module function or user
defined function.
✔ These takes some arguments and returns some
value.
✔ By default these return None.
Advantages
➢It reduces program size. (i.e, Reuse of existing
code)
➢Big and complex program is broken down into
smaller programs (Subprograms).
➢Less memory is required to run program (using
local variables).
Types of Functions
Built in functions (Standard Library Function)
These are pre defined functions which are available
for direct use.
Example – print(), read(), int(), input() etc.
Module functions (Functions inside module)
These functions are used when we import these
modules. import or from keyword is used to import
these modules.
Example
math module → pow(), fabs(), sqrt() etc.
random module → radom(), randrange(), randint()
and choice()
math module functions
import math
print(math.factorial(5)) → 120
print(math.ceil(4.8)) → 5
print(math.floor(4.8)) → 4
print(math.lcm(3,4)) → 12
print(math.gcd(3,4)) → 1
print(math.pow(2,3)) → 8.0
print(math.remainder(11,3)) → -1.0
print(math.sqrt(4)) → 2.0
random module functions
import random
a=random.random() → 0.2155579625
b=random.randint(1,10) → 5
c=random.randrange(1,100,3) → 37
d=random.choice("hello") → o
print(a,b,c,d)
User defined function
These are defined by programmer using def keyword.
It is a group of statements that are represented by a
single unique name.
These mainly work on user input and also return
values.
These require calling or invocation to use.
Syntax -
Example
def hello():
print(“Simple Function”)
print(“Second Statement”)
hello()
hello()
Output
Simple Function
Second Statement
Simple Function
Second Statements
def sum(a,b):
c= a+b
return c
print(sum(4,2))
p=sum(8,9)
Output
6
17
Function Parameter and Arguments
Parameters
These are the variable declared in function
Header/Definition to receive or give default data
values.
Example:
def hello(a, b=10):
Arguments
These are the values given while Function Call.
Example
hello(10)
hello(14,16)
Program for parameters and arguments
def hello(a,b=10):
print(a+b)
hello(10)
hello(14, 16)
Output:
20
30
Types of Arguments
Positional/Required/Mandatory
def hello(a,b):
c=a+b
print(c)
hello(2,4) →Positional Arguments
Default Arguments
def hello(a, b=10, c=30): #Here b and c are Default Arguments.
print(a+b+c)
hello(5)
Keyword (Named) Arguments
def hello (a, b):
print(a+b)
hello(b=2, a=3)
Variable length Argument
def hello(a, *b): #Here b is Variable Length Argument.
print(a, b)
hello(14,2,8,7,1)
Priority:-
Positional Argument > Keyword Argument
Non Default Argument > Default Argument
Returning values from Function (return keyword)
Void/Non-Fruitful/Non Value returning
def hello():
print(“Hello”)
hello()
Non Void/Fruitful/Value returning
def today():
return “Hello world”
print(today())
Possible combination based on Arguments and
Return Type
No Argument + No return Type
No Argument + return Type
Argument + No return Type
Argument + return Type
def hello():
print(“First type”)
hello()
def hello():
print(“It returns 100)
return 100
print(hello())
def hello(a,b):
print(a+b)
hello(4,6)
def hello(a,b):
print(“Argument and return both”)
return(a+b)
print(hello(2,6))
Note:- We can return any type of value from function.
Examples:
return 2+5, return[2,4, “Hello”]
Flow of Execution
Example
1 def hello(a, b):
2 print(a+b0
3 return a+b+4
4 print(“Hello”)
5 t=hello(8,2)
6 print(hello(4,6))
1→4→5→1→2→3→5→6→1→2→3→6
Scope of Variables
Parts of program in which a variable can be used is
called scope of that variable.
Global Scope
Variable that are declared in Top Level Segment (i.e,
main).
These are not declared inside function’s body.
These can be used anywhere within a program.
Local Scope
Variables that are declared inside functions have local
scope.
They cannot be used outside body of a function.
def hello():
a=4 →Local Scope
print(a,p)
p=90 →Global Scope
print(p)
Lifetime of a Variable
The time for which a variable remains in the memory
is called lifetime of a variable.
Global Keyword
Global keyword is used inside a user defined function
definition to create a global variable inside a user
defined function.
def hello():
global a
a=10
m=20
print(a, m)
hello()
print(a, m) #Here a will print value (10) and m will give an error.
Functions with Mutable and Immutable Data
Types
def h(p):
print(p)
for x in range(len(p)):
p[x]=p[x]*2
print(p)
print(id(p))
k=[2,3,4,5,6]
print(k)
h(k)
print(k)
print(id(k))
Output
[2, 3, 4, 5, 6]
[2, 3, 4, 5, 6]
[4, 6, 8, 10, 12]
2637846278
[4, 6, 8, 10, 12]
2637846278
Note:- Here address of k and p are same.
def h(p):
p=list(p)
print(p)
for x in range(len(p)):
p[x]=p[x]*2
print(p)
print(id(p))
k=[2,3,4,5,6]
print(k)
h(k)
print(k)
print(id(k))
Output
[2, 3, 4, 5, 6]
[2, 3, 4, 5, 6]
[4, 6, 8, 10, 12]
2637846278
[4, 6, 8, 10, 12]
2637846297
Note:- Here address of k and p are Different.