12 Functions in Python
12 Functions in Python
Class 12
CBSE
Krish_Info_Tech
•A function is a subprogram
What Is
that acts on data and often
a function?
returns a value.
Krish_Info_Tech
Types of functions
Types of
Functions
Built-in User-Defined
Modules
Functions Functions
Krish_Info_Tech
• These are predefined function in python and are used as
and when there is need by simply calling them.
• Example:
Built-in • int( )
• float( )
functions • str( )
• min( )
• max( )
• print( ) ...etc
Krish_Info_Tech
These functions are pre-defined in particular
modules and can only be used when the
corresponding module is imported.
modules
Eg:
If u want to use pre-defined function sin( ) , you need to
first import the module math.
Krish_Info_Tech
How to import?
• Syntax:
import module_name
• Example:
import math
Krish_Info_Tech
User defined
functions
These are defined by the programmer.
As programmers you can create your
own functions. Krish_Info_Tech
def <function name> ( [ parameters ] ) :
< statement >
Syntax for ..........................
user defined [ < statement > ]
.
function .
.
[ return ]
Krish_Info_Tech
• where,
odef means a function definition starting
oFunction name is the name of the function and it is
Syntax for user an identifier
oParameters are the value to collect the values passed
defined function
during function call
oThe colon (:) at the end of the def line, means it
requires a block.
oThe return statement returns the computed result
Krish_Info_Tech
examples for user defined function
Eg1: def sum ( x,y ) : Eg2: def greet ( ) :
s=x+y print("Hello")
return s
Krish_Info_Tech
Calling/invoking/using a function
<function_name> ( <value-to-be-passed-to-argument> )
Example
sum(10,20)
Krish_Info_Tech
Function Header
Parameters
Indentation
Krish_Info_Tech
Arguments and parameters
• Two types of values which are passed and received during the function and the function definition are:
oArguments – refers to the values being passed
oParameters – refers to the values being received
• Eg: Parameters
def func1( ):
...............................
...............................
func1( )
Arguments Krish_Info_Tech
Actual and formal
parameters
The alternate names for argument are
Actual parameters and for parameter it is Formal
parameters.
Krish_Info_Tech
Passing parameters
Positional
Arguments
( Required
Arguments)
Keyword or
Named
Arguments
Krish_Info_Tech
When the function call statement must match the number and order of
arguments as defined in the function definition, this is called positional
arguments.
Positional
Arguments Eg1: Eg2:
def func1( a,b ): def func1( a,b ):
(Required c=a * b c=a + b
Arguments) print ( c ) return c
func1( "12A",5 ) print( func1(10,20))
Krish_Info_Tech
• Python allows as to assign default value your function's
parameter which is useful in case a matching argument is not
passed in the function called statement.
• The default values are specified in the function header of function
Default definition.
arguments • Eg:
def func1(a,b,c=20):
d=a+b+c
return d
print(funct1(10,20))
Krish_Info_Tech
• A parameter having default value in the function
header is known as default parameter.
• Require parameters should be before default
Default arguments parameters.
• The default values for parameters are considered only
if no value is provided for that parameter in the
function call statement.
Krish_Info_Tech
• Python offers a way of writing function call where you can write any argument
in any order provided you name the argument when calling the function.
• Eg1:
def interest(p=10000,n=20,r=35):
Keyword i=(p*n*r)/100
print I
arguments Interest()
• Eg2:
def interest(n=20,p=10000,r=35):
• Eg3:
def interest(r=35,p=10000,n=20):
Krish_Info_Tech
Rules for combining all three types of arguments
Krish_Info_Tech
Function call – legal / illegal
Function call statement legal / illegal
interest(prin=3000, cc=5) Legal
interest(rate=0.12, prin=3000, cc=5) Legal
interest(cc=5, rate=0.12, prin=3000) Legal
interest(5000, 3, rate=0.05) Legal
interest(rate=0.05, 5000, 3) illegal
interest(5000, prin=300, cc=2) illegal
interest(5000, principal=300, cc=2) illegal
interest(5000, time=2, rate=0.05) illegal Krish_Info_Tech
Return statement
Krish_Info_Tech
Functions in Python may or may not return a value.
functions returning
Returning some value ( non-void
functions )
values from Types of functions
Functions not returning
functions any value ( void
functions )
Krish_Info_Tech
Functions returning some value ( non-void functions )
The functions that return some computed result in terms of a value, fall in this category.
The computer value is returned using return statement. return
Syntax
return <value>
Example
return 5
return 6+4
retuen (a+8**2)/b Krish_Info_Tech
def sum(x,y):
Example s=x+y
program return s
result = sum ( 5 , 3 )
Krish_Info_Tech
Where to use return statement
• The returned value of a function should be used in the caller function/program inside an expression or a statement.
• if you do not use their value in any of the following ways and just give a standalone function call, Python will not report
an error but their return value is completely wasted:
add_result = sum ( a , b )
print ( sum ( 3 , 4 ) )
Krish_Info_Tech
Another use of return statement
The return statement ends a function execution even if it is in the middle of the function.
def check ( a ) :
a = a**2
return a
print ( a )
check(-15)
Krish_Info_Tech
Functions not returning any value ( void functions )
• The functions that perform some action or do some work but do not return any computed value or final value to the
caller are called void functions.
• A void function may or may not have a return statement, then it takes the following form:
return
that is, keyword return without any value or expression.
def greet ( ) :
def greet ( ) :
print ( "hello" )
print ( "hello" )
return
Krish_Info_Tech
• Functions returning a value are known as fruitful function.
Fruitful and non- • A void function, sometimes called non fruitful function, returns legal
fruitful functions empty value of Python i.e., None to its caller.
Krish_Info_Tech
Possible combinations of functions
• non void functions without any arguments
• non void functions with some arguments
• void functions without any arguments
• void functions with some arguments
Krish_Info_Tech
Returning multiple values
• Python lets you return more than one value from a function.
• to return multiple values from a function, you have to ensure following things:
oThe return statement inside a function body should be of the form given below:
return <value1>,<value2>,.....
oThe function call statement should receive or use the returned values in one of the following ways:
▪ either receive the returned values in form of tuple variable
t=squared(2,3,4)
▪ or you can directly unpack the received values of tuple by specifying the same number of variables on the left
hand side of the assignment in function call.
v1,v2,v3=squared(2,3,4) Krish_Info_Tech
Scope of variables
Parts of program within which a name is legal and accessible, is called scope of the
name.
Krish_Info_Tech
global scope
• A name declared in top level segment i.e., the main
program is set to have global scope and is usable
inside the whole program and all blocks contained
within the program.
Types of scopes
local scope
• A name declared in a function body is set to
have local scope that is it can be used only within this
function and the other blocks contained under it.
Krish_Info_Tech
example
Krish_Info_Tech
• Variables defined outside all functions are global variables
Krish_Info_Tech
Name resolution
When you access a variable from within a program or function, Python follows name resolution rule, also known as LEGB rule.
Krish_Info_Tech
Local environment
• It checks within its local environment if it has a variable with the same name; if yes, python uses its value. If not then it
moves to next step.
enclosing environment
• Python now checks the enclosing environment that is if whether there is a variable with the same name otherwise it repeats
this step to the higher level enclosing environment if not then it moves to step 3.
global environment
• Python now checks the global environment whether there is a variable with the same name; if yes Python uses its value, if not
then it moves to Step 4.
built an environment
• Python checks its built in environment that contains all built in variables and functions of Python, if there is a variable with
the same name; if S Python uses its value.
otherwise Python reports an error; name< variable> not defined.
Krish_Info_Tech
Built-in Namespace
4
Global Namespace 3
Enclosing Namespace 2
1
Local Namespace
Krish_Info_Tech
Mutable / immutable properties
of passed data objects
Krish_Info_Tech
Properties
• Pythons 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. That is, if a
variable is referring to an immutable type then any change in its value will also change the memory
address it is referring to, but 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.
Krish_Info_Tech
Mutability / immutability of arguments / parameters and
function calls
• Mutability of arguments / parameters affects the change of value in caller function.
oPassing an immutable type value to a function.
oPassing an mutable type value to a function.
Krish_Info_Tech