Functions in Pythons UDF and Functions Concepts

nitinaees 30 views 44 slides Jul 19, 2024
Slide 1
Slide 1 of 44
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44

About This Presentation

Functions


Slide Content

Working With Functions
Class 12
CBSE
Krish_Info_Tech

What Is
afunction?
•A function is a subprogram
that acts on data and often
returns a value.
Krish_Info_Tech

Types of functions
Types of
Functions
Built-in
Functions
Modules
User-Defined
Functions
Krish_Info_Tech

Built-in
functions
•These are predefined function in python and are used as
and when there is need by simply calling them.
•Example:
•int( )
•float( )
•str( )
•min( )
•max( )
•print( ) ...etc
Krish_Info_Tech

modules
Eg:
If u want to use pre-defined function sin( ) , you need to
first importthe module math.
These functions are pre-defined in particular
modules and can only be used when the
corresponding module is imported.
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

Syntax for
user defined
function
def <function name> ( [ parameters ] ) :
< statement >
..........................
[ < statement > ]
.
.
.
[ return ]
Krish_Info_Tech

Syntax foruser
definedfunction
•where,
odef means a function definition starting
oFunction name is the name of the function and it is
an identifier
oParameters are the value to collect the values passed
during function call
oThe colon (:) at the end of the def line,means it
requires a block.
oThe returnstatement returns the computedresult
Krish_Info_Tech

examples foruserdefinedfunction
Eg1:def sum ( x,y ) :
s = x + y
return s
Eg2: def greet ( ) :
print("Hello")
Krish_Info_Tech

Calling/invoking/using a function
<function_name> ( <value-to-be-passed-to-argument> )
Example
sum(10,20)
Krish_Info_Tech

Define the
terms
•The first line of the function definition that begins with
keyworddefand ends with a colon ( : ), specifies the
name of the function andits parameters.
Function Header
•Variables that are listed within the parameters of a
function header.
Parameters
•The block of statements beneath function header that
defines the actionperformed by the function.
Function Body
•The blank space in the beginning of a statement within a
block. Allstatements within same block have same
indentation.
Indentation
Krish_Info_Tech

Flow of
execution
The flow of execution refers to the order in which statements are
executed during a program run.
A function body isa blockwhich is executedas a unitand is
executedin an executionframe.
An execution framecontains:
•Some internal information
•Name of the function
•Values passed to function
•Variables created within function
•Information about the next instruction to be executed.
Krish_Info_Tech

How a function works
def function( ):
.......................................
.......................................
return
#main ( ) - control begins
….................................
function( ) #function call
print(…...............)
Control moves from function call to function header
Send the control from where the function was called
Krish_Info_Tech

Arguments and parameters
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 thevaluesbeing passed
oParameters – refers to the values being received
•Eg:
def func1( ):
...............................
...............................
func1( )
Parameters
Arguments Krish_Info_Tech

Actual and formal
parameters
The alternate names for argument are
Actualparameters and for parameter it is Formal
parameters.
Krish_Info_Tech

Passing parameters
Types of formal
arguments
Positional
Arguments
( Required
Arguments)
Default
Arguments
Keyword or
Named
Arguments
Krish_Info_Tech

Positional
Arguments
(Required
Arguments)
When the function call statement must match the number and order of
arguments as defined in the function definition,this is called positional
arguments.
Eg1:
def func1( a,b ):
c=a * b
print ( c )
func1( "12A",5 )
Eg2:
def func1( a,b ):
c=a + b
return c
print( func1(10,20))
Krish_Info_Tech

Default
arguments
•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
definition.
•Eg:
def func1(a,b,c=20):
d=a+b+c
return d
print(funct1(10,20))
Krish_Info_Tech

Defaultarguments
•A parameter having default value in the function
header is known as default parameter.
•Require parameters should be before default
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

Keyword
arguments
•Pythonoffers 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):
i=(p*n*r)/100
print I
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
•Python states that in a function call statement
oAn argument list must first contain positional arguments followed by any keyword
arguments.
oKeyword arguments should be taken from the required arguments preferably.
oYou cannot specify a value for an argument more than once.
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

Returning
values from
functions
Functions in Python may or may not return a value.
Types of functions
functions returning
some value ( non-void
functions )
Functions not returning
any value (void
functions )
Krish_Info_Tech

Functions returning some value ( non-voidfunctions )
The functions thatreturnsome computed result in terms of a value,fall in this category.
The computer value is returned using returnstatement.return
Syntax
return <value>
Example
return 5
return 6+4
retuen(a+8**2)/b Krish_Info_Tech

Example
program
def sum(x,y):
s = x + y
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 functionmay or may not have a returnstatement, thenit takes the following form:
return
that is, keyword returnwithout any value or expression.
def greet ( ) :
print ( "hello" )
def greet ( ) :
print ( "hello" )
return
Krish_Info_Tech

Fruitful and non-
fruitful functions
•Functions returning a value are known as fruitful function.
•A void function, sometimes called non fruitful function, returns legal
empty value of Python i.e., None to itscaller.
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 withinwhich a name is legal and accessible,is called scope of the
name.
Krish_Info_Tech

Types of scopes
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.
local scope
•A name declared in a function body is set to
havelocal scope that is it can be used only within this
function and the otherblockscontained under it.
Krish_Info_Tech

example
Krish_Info_Tech

•Variables defined outside all functions are globalvariables
Krish_Info_Tech

Name resolution
When you access a variable from within a program or function,Python follows name resolution rule,also known as LEGBrule.
Local environment
Enclosing
environment
Global
environment
Built in
environment
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
Global Namespace
Enclosing Namespace
Local Namespace
1
2
3
4
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
Tags