CBP (CYCLE–II) FOR SENIOR SECONDARY LEVEL TEACHERS
(CLASS XI AND XII)
SUBJECT – COMPUTER SCIENCE
DATE - 26 AUGUST 2020
TOPIC – FUNCTIONS IN PYTHON
PRESENTED BY :
Ms. Geetanjali Hota
DAV Public School,
Pokhariput,BBSR
Work is Worship 1
✓ PARTICIPANTS ARE REQUESTED TO KEEP THEIR AUDIO SYSTEM
ON MUTE-MODE AND VIDEO ON .
✓ USE CHAT BOX FOR ALL RESPONSES AND QUERIES.
✓ USE EAR-PHONES OR HEADSETS FOR BETTER AUDIO CLARITY.
✓ KINDLY, DO NOT KEEP YOURSELF BUSY \IN TAKING
SCREENSHOTS. PPT SHALL BE SHARED TO DAV CAE IN DUE
COURSE.
Work is Worship 2
D o n’t
R epeat
Y ourself
Work is Worship 2
A function is a subprogram that acts on
data and often returns a value.
Work is Worship 3
In the given polynomial: 2x2
For x=1, it will give the result as 2x12=2
For x=2, it will give the result as 2x22=8
For x=3, it will give the result as 2x32=18
Now if we will represent the above polynomial like
f(x)= 2x2 What you observe:-
Then we can say that, 1. A value is needed (argument)
2. Can perform certain operation
f(1)=2 3. Can return a result
f(2)=8
f(3)=18
Work is Worship
Broadly Python functions can belong to one of the
following three categories:-
1. Built-in function:- These are pre defined
functions and are always available for use.
2. Function defined in modules:- These functions
are pre defined in particular module and is
imported.
3. User defined function:- These are defined by the
programmer as per the specific requirement.
Work is Worship
variable
The above mentioned mathematical function f(x)
can be written in Python as:-
def Calculate(x):
r=2*x**2
return r
Where,
➢ def means a function definition starting
➢Identifier following ‘def’ is the name of the function, i.e.
here the function name is Calculate()
➢The variable x inside the parenthesis is
argument/parameter.
➢There is a colon after def, meaning it requires a block.
➢The statement indented below the function defines the
functionality of the function. It is called as body-of-the
function.
➢The return statement returns the result.
Work is Worship
Work is Worship
Keyword Function Parameter
name
Body of the Function
Function calling
Work is Worship
Sample Code-1
Taking one parameter
and has return value
Sample Code-2
Taking two parameters
and has no return
value
Work is Worship
In Python program, generally all function definitions are given at
the top followed by statements which are not part of any function.
These are called as top level statements (the one with no
indentation). The python interpreter starts the execution of the
program from the top level statement. Python gives a special name
to top level statements as __main__.
Example:-
1
2 Top level
statement
3
4
Work is Worship
The flow of execution refers to the order in which the statements
are executed during a program run.
def Func():
.
.
return
#__main__
.
.
Func()
print(…)
Work is Worship
Answer
5→6→7→2→3→4→7→8
Work is Worship
Answer
5→6→7→2→3→4
Work is Worship
Argument appears in function call statement.
Parameter appears in function header.
The alternate name for arguments are Actual
Argument and parameter is Formal parameter.
Example:-
▪10,11 are called as
Argument/ Actual
Argument
•x,y are called as
parameter/ formal
parameter.
Work is Worship
Argument in Python can be a:-
Literal
Variable
Expressions
But, parameters have to be some names.
Thus, for the function as defined below:-
def Multiply(a,b):
return (a*b)
Find the valid function call statements:-
1) Multiply(3,4) Valid
2) P=9 Valid
Multiply(P,5)
3) Multiply(P,P+1) Valid
Work is Worship
Python supports three types of formal
argument/ parameter:-
Positional Argument (Required Argument)
Default Argument (Optional Argument)
Keyword Argument (Names Argument)
Work is Worship
You need to match the number of arguments with number
of parameters required. For example if the function
definition is:
def Example(a,b,c)
The possible call to this function is
Example(x,y,z)
Example(3,y,z)
Example(3,4,z)
Example(3,4,5)
This way of argument specification is called Positional
Argument or Required Argument or Mandatory Argument
as no value can be skipped while calling the function.
Work is Worship
Default argument is a method of assigning some default
value to the function parameter. The parameter having
default value in function header is known as a default
parameter.
For example:
def Example(a,b,c=10) Default Argument
The possible call to this function is
Example(x,y,z)
Example(x,y)
Work is Worship
Work is Worship
def SI(P,T,R=0.10): Valid
Invalid
def SI(P,T=2,R):
Invalid
def SI(P=2000,T=2,R):
def SI(P,T=2,R=0.10): Valid
def SI(P=2000,T=2,R=0.10): Valid
Work is Worship
def example(a,b,c=10,d=20):
Justify
1. example(30, 35) #correct
2. example(90) #incorrect
3. example(90,87,100) #correct
4. example(90,87,20,30) #correct
5. example( ) #incorrect
Work is Worship
Keyword arguments are the named arguments with assigned values being
passed in the function call statement.
Python provides a way of writing function call where you can write any
argument in any order provided you name the arguments when calling
the function.
For example:-
def SI(P,R,T):
The above function can be called by-
SI(P=2000,T=2,R=0.02)
SI(T=4,P=2000,R=0.02)
SI(T=2,R=0.09,P=2000)
Work is Worship
An argument list must first contain positional
argument followed by any keyword argument.
Keyword arguments should be taken from the
required arguments preferably.
You can not specify a value for an argument
more than once.
Work is Worship
An argument list must first contain positional
argument followed by any keyword argument.
Keyword arguments should be taken from the
required arguments preferably.
You can not specify a value for an argument more
than once.
Having positional arguments after keyword
arguments will result into error.
Work is Worship
Find legal/ illegal function call statement??
1
3
Consider the following function
header:- 4
def Example(a,b,c=2,d=0.2): 5
return (a+b+c+d)
Work is Worship
Answer
Function call statement Legal/ Reason
Illegal
Example(a=3000,b=5) Legal
Example(d=0.12,a=3000,b=4) Legal
Example(b=4,d=0.12,a=3000) Legal
Example(5000,5,d=0.45) Legal
Example(d=0.56,3000,5) Illegal Keyword argument before
positional argument
Example(5000,a=200,b=6) Illegal Multiple values provided for a.
once as positional argument and
again as keyword argument.
Example(5000,e=500,b=7) Illegal Undefined name used. e is not a
parameter.
Example(600,c=6,d=0.06) Illegal A required argument b is
missing.
Work is Worship
There broadly 2 types of return in python
◦ Function returning some value (non-void function)
◦ Function not returning any value (void function)
Work is Worship
The function that return some computed result in
terms of a value, fall in this category. The
computed value is returned using return statement
as per syntax:-
return <value>
The values being returned can be one of the
following:
◦ A literal Some legal return statement
◦ A variable
◦ An expression
Work is Worship
The return value of a function should be used in the
caller function/ program inside an expression or a
statement.
•Res=sum(a,b)
•print(sum(10,20))
•sum(10,20)>6
Work is Worship
The return statement ends a function execution even if
it is in the middle of the function. A function ends at
the moment when it reaches a return statement.
This statement
is unreachable
Work is Worship
•The function that perform some action, but do not return any
computed value to the caller are called void functions.
•A void function may or may not have a return statement.
Work is Worship
•A void function do not return a value but, they
return a legal empty value, i.e None.
Output:-
Work is Worship
•When a function returns multiple values, then either receive the
returned value in form of Tuple variable or directly unpack the
received value of Tuple by specifying same number of variables
on the LHS.
Tuple T will be Output will be
printed as (1,4,9) printed as 1 4 9
Work is Worship
Work is Worship
The scope rule of a language are the rules that
decide, in which part(s) of the program, a
particular piece of code or data item would be
known and can be accessed there in.
There are broadly two types of scopes in
python:--
-Global Scope
-Local Scope
Work is Worship
A name declared in top level segment (__main__) of a
program is said to have global scope and is usable
inside the whole program and all blocks (functions and
other blocks) contained within the program.
Here ‘a’ has global scope
and ‘a’ is called as a
Global Variable
Work is Worship
A name declared in a function body is said to have
local scope i.e. it can be used only within this function
and the other blocks contained under it. The formal
arguments also have local scope.
Here ‘a’ has local scope
and ‘a’ is called as a
Local Variable
Work is Worship
Global Environment
1. def Sum(a,b):
no1=10
2. res=a+b
no2=15
3. return res
4. no1=int(input(“Enter first no:”))
Local Environment
5. no2=int(input(“Enter second no:”))
a=10
6. s=Sum(a,b)
b=15
7. print(“Sum=“,s)
res=25
Work is Worship
Python follows the name resolution rule, known as
LEGB Rule.
L Local Environment
E Enclosing Environment
G Global Environment
B Built-in Environment
Work is Worship
Here ‘a’ is a Local Variable
Here ‘x’ is a Global Variable
Output is
15 10
10
Work is Worship
Here ‘x’ is neither defined in local
scope nor defined in global scope
Work is Worship
Here ‘x’ is in both local scope and
in global scope
Output is
7
15
Work is Worship
Use global statement in the program.
Output is
95
15
15
T I P:-
Although Global Variables can be
accessed through local scope, but it
is not a good programming practice.
So, keep global variable global and
local variable local.
Work is Worship
➢If a variables is referring to an immutable type then any change
in its value will also change the memory address it is referring to.
➢If a variables is referring to a mutable type then any change in
the value of mutable type will not change the memory address of
the variable.
Mutable Immutable
Parameter Parameter
Work is Worship
Lists are mutable data object
Output is
[10, 20, 30]
[12, 20, 30]
[12, 20, 30]
Output is
[10, 20, 30]
[60, 70]
[10, 20, 30]
Work is Worship
Dictionary is mutable data object
Output is
{1: 'Red', 3: 'White', 8: 'Blue'}
{1: 'Red', 3: 'Indigo', 8: 'Blue'}
{1: 'Red', 3: 'Indigo', 8: 'Blue'}
Output is
{1: 'Red', 3: 'White', 8: 'Blue'}
{7: 'CPU', 9: 'Mouse'}
{1: 'Red', 3: 'White', 8: 'Blue'}
Work is Worship
String is an immutable data object
Output is
Laptop
Desktop
ERROR
Work is Worship
Tuples are immutable data object
Output is
(10, 20, 30)
[60, 70]
(10, 20, 30)
ERROR
Work is Worship
Changes in immutable types are not reflected in the
caller function at all.
Changes, if any, in mutable types
◦ Are reflected in caller function if its name is not assigned a
different variable or data types.
◦ Are not reflected in the caller function if it is assigned a
different variable or data types.
Work is Worship
STAY HAPPY AND
STAY SAFE
Work is Worship