Functions
Functions
BY PRAJEESH M
FUNCTION
()
GROUP OF STATEMENTS (block)
COMBINED TOGETHER WITH A NAME
DO A PARTICULAR TASK
POSITIVES
CODE REUSABILITY
USED TO REDUCE PGM SIZE
MAKE PGM MORE READABLE AND
UNDERSTANDABLE.
SAVES TIME
TYPES OF FUNCTIONS
USER
DEFINED
FUNCTIONS
IN MODULES sqrt(),sin()
USER DEFINED FUNCTIONS
CREATION OF A FUNCTION IS CALLED FUNCTION
DEFINTION
ACCESSING OF A FUNCTION IS CALLED FUNCTION
CALLING
CREATION OF USER DEFINED FUNCTIONS
• FUNCTION DEFINITION
• SYNTAX:
• Values/parameters
specified at the
Formal time of function
definition
Parameters
• Values/parameters
specified at the
Actual time of function
call
ACCESSING A FUNCTION
• FUNCTION CALLING
• SYNTAX:
• functionname(ACTUAL_parameters)
Eg:
def fact(a):
f=1
for i in range(1,a+1):
f=f*i
print(f)
fact(5)
5actual parameter
a formal parameter
return a
return 'a'
return a+b
return (a+b)/10
s=9*23
return s
1.POSITIONAL/REQUIRED/MANDATORY
def sum(a,b,c):
print(“Sum is”,a+b+c)
d,e,f=10,20,30
sum(d,e,f)
or
sum(10,20,30)
or
sum(10,20+30,d+e+f)
sum() ×
2.Default argument
def sum(a,b=100,c=200):
print(“Sum is”,a+b+c)
d,e,f=10,20,30
sum(d,e,f)60
sum(d,e) 230
sum(d)310
sum() ×
3.KEYWORD /NAMED ARGUMENTS
def sum(theory,practical)
sum(70,30)
sum(practical=30,theory=70)
4.Variable length argument
def just(*h)
print(h)
just(1,2,3,45)
*data stored as tuples
Scope of variables
Parts of a program in which variables can be used
1. Local scope
* Variable that defined/declared inside a function or
block
* Can be used/ accessed only within that function
2. Global scope
* Outside all functions
* Can be used/ accessed anywhere in that program
Life time of a variable:
Time for which variable remains in the memory
Global variables can be created before/after all
function definitions
Eg
a=10
def hello():
print(a)
hello()10
print(a)10
def hello():
x=“hello”
print(a)
a=10
hello()10
print(x)×
print(a)
Use of global keyword
def state1():
tigers=15
print(tigers)
tigers=95
print(tigers)
state1()
print(tigers)
---------------------------------------------------------
def state1():
global tigers
tigers=15
print(tigers)
tigers=95
print(tigers)
state1()
print(tigers)
Flow of execution
1. def hello():
2. print(“Hello”)
3. def hi():
4. print(“hi”)
5. hello()
6. hi()
7. print(“Bye”)
1-->3-->6-->3-->4-->5-->1-->2-->6-->7
Random module
import random
print(random.random())---->0-1(.23, .99)(including 0
excluding 1+takes no arg)
print(random.randint(1,7))--->1 and 7 included {Accept 2
para}
print(random.randrange(1,7))--->7 not included
print(random.randrange(1,7,2))--->7 not included[1,3,5]
print(random.choice('computer'))
print(random.choice([2,56,45,90,34]))
print(random.uniform(1,100)[1 inclu 100 exclu+float
values]
Eg:
Some useful inbuilt functions
math module:
import math
print(math.sqrt(9))3.0
print(math.pow(2,3))8.0
print(math.floor(42.45))42 ,int
print(math.ceil(42.4))43,int
print(math.fabs(-9))9.0
round(12.53)--int, 13
round(12.43)--int, 12
round(12.53,0)--float, 13.0
round(12.43,0)--float, 12.0
round(12.53,1)--float, 12.53
round(12.4378,2)--float, 12.44