Working With Functions-2
Working With Functions-2
Working With Functions-2
com
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
Introduction
Large programs are often difficult to manage, thus
large programs are divided into smaller units known as
functions.
It is simply a group of statements under any name i.e.
function name and can be invoked (call) from other part
of program.
Take an example of School Management Software, now
this software will contain various tasks like Registering
student, Fee collection, Library book issue, TC
generation, Result Declaration etc. In this case we have
to create different functions for each task to manage
the software development.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Introduction
Set of functions is stored in a file called MODULE.
And this approach is known as MODULARIZATION,
makes program easier to understand, test and
maintain.
Commonly used modules that contain source code
for generic need are called LIBRARIES.
Modules contains set of functions. Functions is of
mainly two types:
Built-in
Functions
User-Defined Functions
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Advantages of Function
PROGRAM HANDLING EASIER : only small part of the
program is dealt with at a time.
REDUCED LoC: as with function the common set of code
is written only once and can be called from any part of
program, so it reduces Line of Code
EASY UPDATING : if function is not used then set of
code is to be repeated everywhere it is required.
Hence if we want to change in any formula/expression
then we have to make changes to every place, if
forgotten then output will be not the desired output.
With function we have to make changes to only one
location.
Points to remember…
Keyword def marks the start of function header
Function name must be unique and follows naming rules
same as for identifiers
Function can take arguments. It is optional
A colon(:) to mark the end of function header
Function can contains one or more statement to perform
specific task
An optional return statement to return a value from the
function.
Function must be called/invoked to execute its code
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
def max(x,y):
if x>y:
return x
else:
return y
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
print(“Iam not reachable”)
for more updates visit: www.python4csip.com
FORMAL ARGUMENT
ACTUAL ARGUMENT
Types of Arguments
There are 4 types of Actual Arguments allowed in
Python:
1. Positional arguments
2. Default arguments
3. Keyword arguments
4. Variable length arguments
Positional arguments
Are arguments passed to a function in correct
positional order
Default arguments
Sometimes we can provide default values for our
positional arguments. In this case if we are not
passing any value then default values will be
considered.
Default argument must not followed by non-default
arguments.
def interest(principal,rate,time=15): VALID
def interest(principal,rate=8.5,time=15):
def interest(principal,rate=8.5,time): INVALID
Default arguments
Default arguments
Keyword(Named) Arguments
The default keyword gives flexibility to specify
default value for a parameter so that it can be
skipped in the function call, if needed. However, still
we cannot change the order of arguments in
function call i.e. you have to remember the order of
the arguments and pass the value accordingly.
To get control and flexibility over the values sent as
arguments, python offers KEYWORD ARGUMENTS.
This allows to call function with arguments in any
order using name of the arguments.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Keyword(Named) Argument
def Average(n1,n2,n3=100):
return (n1+n2+n3)/3
FUNCTION CALL LEGAL/ REASON
ILLEGAL
Average(n1=20, n2=40,n3=80) LEGAL Non default values provided as
named arguments
Average(n3=10,n2=7,n1=100) LEGAL Keyword argument can be in any
order
Average(100,n2=10,n3=15) LEGAL Positional argument before the
keyword arguments
Average(n3=70,n1=90,100) ILLEGAL Keyword argument before the
positional arguments
Average(100,n1=23,n2=1) ILLEGAL Multiple values provided for n1
Average(200,num2=90,n3=11) ILLEGAL
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR & Undefined argument NUM2
SACHIN BHARDWAJ, PGT(CS), KVILLEGAL
Average(21,num3=900) NO.1 TEZPUR A required argument n2 is missing
for more updates visit: www.python4csip.com
Composition
Refers to using an expression as a part of large
expression, or a statement as a part of large
statement.
Examples
Max((a+b),(c+a)) # Arithmetic
Prize(Cardor Cash) # Logical
name="Vikram“
print(name.replace("m","nt").upper()) #function
Scope of Variables
SCOPE means in which part(s) of the program, a
particular piece of code or data is accessible or
known.
In Python there are broadly 2 kinds of Scopes:
Global Scope
Local Scope
Global Scope
A name declared in top level segment(__main__) of
a program is said to have global scope and can be
used in entire program.
Variable defined outside all functions are global
variables.
Local Scope
A name declare in a function body is said to have
local scope i.e. it can be used only within this
function and the other block inside the function.
The formal parameters are also having local scope.
Let us understand with example….
Lifetime of Variable
Is the time for which a variable lives in memory. For
Global variables the lifetime is entire program run
i.e. as long as program is executing. For Local
variables lifetime is their function‟s run i.e. as long
as function is executing.
Program with
variable “value” in
both LOCAL and
GLOBAL SCOPE
Program with
variable “value” in
both LOCAL and
GLOBAL SCOPE
Using GLOBAL
variable “value” in
local scope
Using GLOBAL
variable “value” in
local scope
Variable “value”
neither in local nor
global scope
Variable “value”
neither in local nor
global scope
Variable in Global
not in Local
(input in variable at
global scope)
Variable in Global
not in Local
(input in variable at
global scope)
Mutability/Immutability of
Arguments/Parameters and function call
Mutability/Immutability of
Arguments/Parameters and function call
Mutability/Immutability of
Arguments/Parameters and function call
From the previous example we can recall the
concept learned in class XI that Python variables
are not storage containers, rather Python variables
are like memory references, they refer to memory
address where the value is stored, thus any change
in immutable type data will also change the
memory address. So any change to formal
argument will not reflect back to its
corresponding actual argument and in case of
mutable type, any change in mutable type will
not change the memory address of variable.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Mutability/Immutability of
Arguments/Parameters and function call
Most non-python
programmers are having the
habit of writing main()
function where the important
and starter code of programs
are written. In Python we can
also create main() and call it
by checking __name__ to
__main__ and then call any
function, in this case main()
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Recursion
It is one of the most powerful tool in programming
language. It is a process where function calls itself
again and again.
Recursion basically divides the big problem into small
problems up to the point where it can be solved easily,
for example if we have to calculate factorial of a 5, we
will divide factorial of 5 as 5*factorial(4), then
4*factorial(3), then 3*factorial(2), then 2*factorial(1)
and now factorial of 1 can be easily solved without any
calculation, now each pending function will be executed
in reverse order.
VINOD KUMAR VERMA, PGT(CS), KV OEF KANPUR &
SACHIN BHARDWAJ, PGT(CS), KV NO.1 TEZPUR
for more updates visit: www.python4csip.com
Example - Recursion
Example - Recursion