Functions: Using Python
Salil Kumar Verma
Jawahar Navodaya Vidyalaya, Adilabad(T.S)
salilverma80@gmail.com
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 1/22
Outline
Function Definition, syntax, types
Arguments/Parameters, types
Void and non-void function
Scope of a variable, global/local, LEGB rule
Flow of execution of a program
Passing immutable/mutable objects
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 2/22
Function
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 3/22
Function
A function is a portion of code within a larger
program that performs a specific task.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 3/22
Function
A function is a portion of code within a larger
program that performs a specific task.
Functions are useful in reusing the code and
eliminate code redundancy.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 3/22
Function
A function is a portion of code within a larger
program that performs a specific task.
Functions are useful in reusing the code and
eliminate code redundancy.
Functions are also used to organise our code into
manageable blocks.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 3/22
Syntax for functions
def function_name(parameters):
"""docstring"""
statement(s)
return [expression]
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 4/22
Docstring
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 5/22
Docstring
Documenting/commenting code is a good practice
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 5/22
Docstring
Documenting/commenting code is a good practice
Docstrings are triple quoted comments entered just
after function definition
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 5/22
Docstring
Documenting/commenting code is a good practice
Docstrings are triple quoted comments entered just
after function definition
It implies what the function does
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 5/22
Example: Function
f(x) a mathematical function
f (x) = x 2
Above mentioned mathematical function can be written
in Python like this:
def f(x):
return x*x
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 6/22
Anatomy of Python function
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 7/22
Python Function types
pre-defined functions and are
Built-in always available for use. e.g.
len(), type(), int(), input() etc.
pre-defined in particular modules
defined in
and can only be used when the
modules
corresponding module is imported
User-
defined by programmer
defined
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 8/22
Arguments and parameters
Consider following program segment
def multiply(a,b): #Function header
print(a*b)
y=3
multiply(12,y) #Function call 1
multiply(y,x) #Function call 2
value(s) in function header → Parameters/Formal
Arguments
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 9/22
Arguments and parameters
Consider following program segment
def multiply(a,b): #Function header
print(a*b)
y=3
multiply(12,y) #Function call 1
multiply(y,x) #Function call 2
value(s) in function header → Parameters/Formal
Arguments
value(s) in function call → Arguments/Actual
Arguments
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 9/22
Passing parameters
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 10/22
Passing parameters
1 Positional arguments (Required arguments)
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 10/22
Passing parameters
1 Positional arguments (Required arguments)
2 Default arguments
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 10/22
Passing parameters
1 Positional arguments (Required arguments)
2 Default arguments
3 Keyword (or named ) arguments
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 10/22
Positional/Required Arguments
if a function definition header is like:
def check(a,b,c):
.
.
.
then possible function calls for this can be:
check(x,y,z) # 3 values(all variables) passed
check(2,x,y) # 3 values (literal+variable) passed
check(2,5,7) # 3 values(all literals) passed
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 11/22
Default Arguments
Example of function header with default values:
def interest(principal,time,rate=0.10):
Function calls for the function:
si_int=interest(5400,2) #third argument missing
si_int=interest(6100,3,0.15) #no argument missing
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 12/22
Keyword (Named) Arguments
If function header is:
def interest(prin,time,rate):
Function calls using keyword arguments:
interest(prin=2000,time=2,rate=0.10)
interest(time=4,prin=2000,rate=0.10)
interest(time=2,rate=0.12,prin=2000)
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 13/22
Using multiple argument types together
Consider following function header:
def interest(prin,cc,time=2,rate=0.09):
return prin*time*rate
Some function call statements:
interest(cc=4,rate=0.12,prin=5000) (legal)
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 14/22
Using multiple argument types together
Consider following function header:
def interest(prin,cc,time=2,rate=0.09):
return prin*time*rate
Some function call statements:
interest(cc=4,rate=0.12,prin=5000) (legal)
interest(rate=0.05,5003) (illegal)
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 14/22
Using multiple argument types together
Consider following function header:
def interest(prin,cc,time=2,rate=0.09):
return prin*time*rate
Some function call statements:
interest(cc=4,rate=0.12,prin=5000) (legal)
interest(rate=0.05,5003) (illegal)
interest(5000,prin=300,cc=2) (illegal)
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 14/22
Returning values from function
Functions returning some values (non-void
functions)
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 15/22
Returning values from function
Functions returning some values (non-void
functions)
Functions not returning any value(void functions)
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 15/22
Returning values from function
Functions returning some values (non-void
functions)
Functions not returning any value(void functions)
Functions returning multiple values
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 15/22
Scope of variables
Scope
Part(s) of program within which a name is legal and
accessible.
Global Scope
A name declared in top level segment( main ) of a
program, it can be used inside whole program and all
blocks(functions,other blocks).
Local Scope
A name declared in a function-body is said to have local
scope, i.e. it can be used only within this function.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 16/22
Scope Example I
Program
1. def calcsum(x,y):
2. z=x+y
3. return z
4. num1=int(input("Enter first number:"))
5. num2=int(input("Enter second number:"))
6. sum=calcsum(num1,num2)
7. print("Sum of given number is ",sum)
Flow of execution of above program:
Line1 Line4 Line5 Line6 Line2 Line3 Line6 Line7
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 17/22
Scope Example II
Program:
1. def calcsum(a,b,c):
2. s=a+b+c
3. return s
4. def average(x,y,z):
5. sm=calcsum(x,y,z)
6. return sm/3
7. num1=int(input("Number 1:"))
8. num2=int(input("Number 2:"))
9. num3=int(input("Number 3:"))
10. print("Average is:",average(num1,num2,num3))
Flow of execution of above program:
Line7
Line2 Line5
Line1 Line4 Line8 Line10 Line5 Line10
Line3 Line6
Line9
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 18/22
Name Resolution (LEGB Rule)
Local Enclosing Global Built-in
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 19/22
Mutable/Immutable properties of passed data
objects
Python’s variables are not storage containers,
rather Python variables are like memory references;
they refer to the memory address where the value
is stored.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 20/22
Mutable/Immutable properties of passed data
objects
Python’s variables are not storage containers,
rather Python variables are like memory references;
they refer to the memory address where the value
is stored.
Depending upon the mutability/immutability of its
data type, a variable behaves differently.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 20/22
Passing an immutable/mutable data type
Passing an immutable type value to a function
If a variable is referring to an immutable type then any
changes in its value will also change the memory
address it is referring to.
Passing a mutable type value to a function
If a variable is referring to mutable type then any
change in the value of mutable type will not change the
memory address of the variable.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 21/22
Summary of mutable/immutable type’s behaviour
Changes in immutable types are not reflected in
the caller function at all.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 22/22
Summary of mutable/immutable type’s behaviour
Changes in immutable types are not reflected in
the caller function at all.
Changes,if any, in mutable types
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 22/22
Summary of mutable/immutable type’s behaviour
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 datatype.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 22/22
Summary of mutable/immutable type’s behaviour
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 datatype.
are not reflected in the caller function if it is
assigned a different variable or datatype.
Salil Kumar Verma, PGT IT, J N V Adilabad Functions 22/22
SCHOOL OF EDUCATORS
You will get Pre- Board Papers PDF, Word file, PPT, Lesson Plan, Worksheet, practical tips and Viva
questions , reference books , smart content , curriculum , syllabus , marking scheme , toppers answer
scripts , revised exam pattern , revised syllabus , Blue Print etc. here .Join Your Subject WhatsApp
Group.
Kindergarten
Class 1 Class 2 Class 3 Class 4
Class 5 Class 6 Class 7 Class 8
Class 9 Class 10 Class 11 (Science) Class 11 (Commerce)
Class 11 (Humanities) Class 12 (Science) Class 12 (Commerce) Class 12 (Humanities)
Subject Wise Groups Secondary and Senior Secondary
Secondary Groups (IX & X)
SST Mathematics Science English
Hindi Information Techonology (402)
Senior Secondary Groups (XI & XII)
Physics Chemistry English Mathematics
Biology Accountancy Economics BST
History Geography Sociology Hindi Elective
Hindi Core Home Science Sanskrit Psychology
Political Science Painting Vocal Music Comp. Science
IP Physical Education App. Mathematics IIT /NEET
Leagal Studies SOE CBSE Principals (Group for Principals Only)
Teachers Jobs
Rules & Regulations of the Group
1. No introduction
2. No Good Morning/Any wish type message
3.No personal Chats & Messages
4. No Spam
5. You can also ask your difficulties here.
Just get learning resources & post learning resources.
Helpline number only WhatsApp: +91-95208-77777
Why Artham Resource Material?
Resource materials for teachers and students are essential tools for effective teaching
and learning. They provide valuable information, guidance, and support to both teachers
and students, making the teaching and learning process more efficient and productive.
For teachers, Artham resource materials include lesson plans, instructional guides,
assessment tools, professional development materials, and teaching aids. These
materials are well researched and created according to 2023-24 NEP and NCERT
guidelines.
For students, resource materials can include textbooks, study guides, homework
assignments, reference books, online learning platforms, and educational videos. These
materials can be obtained from school libraries, educational publishers, online
resources, and teachers.
Both teachers and students can also benefit from Artham educational resources which
are free and openly licensed educational materials that can be used and shared for
teaching and learning. Artham resource material include textbooks, courses, lesson
plans, and multimedia resources that are available online.
In summary, resource materials are critical components of effective teaching and
learning. They provide a wealth of information and support that can enhance the quality
of education and help students achieve academic success.
Teachers and students can also purchase these resources from the links provided with
every resource.
JOIN TELEGRAM GROUP/CHANNELS FOR
CLASS WISE HIGH QUALITY RESOURCE
MATERIAL
SOE CBSE Groups
Click to Join CBSE Group...All classes
Click to Join SOE CBSE Kindergarten Group
Click to Join SOE CBSE Class 1 Group
Click to Join SOE CBSE Class 2 Group
Click to Join SOE CBSE Class 3 Group
Click to Join SOE CBSE Class 4 Group
Click to Join SOE CBSE Class 5 Group
Click to Join SOE CBSE Class 6 Group
Click to Join SOE CBSE Class 7 Group
Click to Join SOE CBSE Class 8 Group
Click to Join SOE CBSE Class 9 Group
Click to Join SOE CBSE Class 10 Group
Click to Join SOE CBSE Class 11 (Science) Group
Click to Join SOE CBSE Class 11 (Commerce) Group
Click to Join SOE CBSE Class 11 (Humanities) Group
Click to Join SOE CBSE Class 12 (Science) Group
Click to Join SOE CBSE Class 12(Commerce) Group
Click to Join SOE CBSE Class 12 (Humanities) Group
Click to Join SOE JEE/NEET Group
Click to Join SOE CUET Group
Click to Join SOE NDA, OLYMPIAD, NTSE Group
Click to Join SOE School Principal Professional Development Group
Click to Join SOE School Teacher Professional Development Group
Click to Join SOE CBSE Project File Group for Class 9th to 12th All Subjects
SOE ICSE Groups
Click to Join SOE ICSE Kindergarten Group
Click to Join SOE ICSE Class 1 Group
Click to Join SOE ICSE Class 2 Group
Click to Join SOE ICSE Class 3 Group
Click to Join SOE ICSE Class 4 Group
Click to Join SOE ICSE Class 5 Group
Click to Join SOE ICSE Class 6 Group
Click to Join SOE ICSE Class 7 Group
Click to Join SOE ICSE Class 8 Group
Click to Join SOE ICSE Class 9 Group
Click to Join SOE ICSE Class 10 Group
Click to Join SOE ICSE Class 11 (Science) Group
Click to Join SOE ICSE Class 11 (Commerce) Group
Click to Join SOE ICSE Class 11 (Humanities) Group
Click to Join SOE ICSE Class 12 (Science) Group
Click to Join SOE ICSE Class 12(Commerce) Group
Click to Join SOE ICSE Class 12 (Humanities) Group
Click to Join SOE JEE/NEET Group
Click to Join SOE CUET Group
Click to Join SOE NDA, OLYMPIAD, NTSE Group
Click to Join SOE School Principal Professional Development Group
Click to Join SOE School Teacher Professional Development Group
Nageen CBSE Channels
Click to Join Nageen CBSE Kindergarten Channel
Click to Join Nageen CBSE Class 1 Channel
Click to Join Nageen CBSE Class 2 Channel
Click to Join Nageen CBSE Class 3 Channel
Click to Join Nageen CBSE Class 4 Channel
Click to Join Nageen CBSE Class 5 Channel
Click to Join Nageen CBSE Class 6 Channel
Click to Join Nageen CBSE Class 7 Channel
Click to Join Nageen CBSE Class 8 Channel
Click to Join Nageen CBSE Class 9 Channel
Click to Join Nageen CBSE Class 10 Channel
Click to Join Nageen CBSE Class 11 (Science) Channel
Click to Join Nageen CBSE Class 11 (Humanities) Channel
Click to Join Nageen CBSE Class 11 (Commerce) Channel
Click to Join Nageen CBSE Class 12 (Science) Channel
Click to Join Nageen CBSE Class 12 (Commerce) Channel
Click to Join Nageen CBSE Class 12 (Humanities) Channel
Click to Join JEE/NEET Channel
Click to Join CUET Channel
Click to Join NDA, OLYMPIAD, NTSE Channel
Nageen ICSE Channels
Click to Join Nageen ICSE Kindergarten Channel
Click to Join Nageen ICSE Class 1 Channel
Click to Join Nageen ICSE Class 2 Channel
Click to Join Nageen ICSE Class 3 Channel
Click to Join Nageen ICSE Class 4 Channel
Click to Join Nageen ICSE Class 5 Channel
Click to Join Nageen ICSE Class 6 Channel
Click to Join Nageen ICSE Class 7 Channel
Click to Join Nageen ICSE Class 8 Channel
Click to Join Nageen ICSE Class 9 Channel
Click to Join Nageen ICSE Class 10 Channel
Click to Join Nageen ICSE Class 11 (Science) Channel
Click to Join Nageen ICSE Class 11 (Commerce) Channel
Click to Join Nageen ICSE Class 11 (Humanities) Channel
Click to Join Nageen ICSE Class 12 (Science) Channel
Click to Join Nageen ICSE Class 12 (Commerce) Channel
Click to Join Nageen ICSE Class 12 (Humanities) Channel
Click to Join JEE/NEET Channel
Click to Join CUET Channel
Click to Join NDA, OLYMPIAD, NTSE Channel