Professional Documents
Culture Documents
Lecture 8
Lecture 8
Functions ( )
In large programs, we try to avoid a single large list of instructions.
It’s broken down into smaller units known as Functions.
A Function is a subprogram/mini-program.
What is a Function?
When we know some things are repetitive or very similar.
To avoid writing same code again and again, we write it once and give it a
name.
This name is the function name, and whenever we call this name the code in
it will run.
Types of Functions
1. User Defined Functions : Designed by Programmers, like we will make.
def sum(x, y) :
print(x+y) parameters/arguments
Function Body
Structure of a Python Program(not imp.)
def function1( ) :
:
def function2( ) :
: Top to Bottom
#top level statements
statement1
statement2
:
Interpreter start _main_ se karta hai
Flow of Execution(learn the flow)
def fun( ) :
print(‘Hello’)
:
print(‘Babye’) Top to Bottom
:
fun( )
:
:
Interpreter start _main_ se karta hai
Practice Time
multiply(3,5) arguments
Arguments & Parameters
Arguments can be literals, variables, expressions.
But Parameters have to be some name/identifier, variable to hold incoming
values.
calculate( 2, 4, 1 )
calculate( x, 8, 3 )
calculate( p, q, r )
calculate( 2, 1)
calculate( 5 )
food(‘Paneer’, ‘Pulaao’)
food(‘Pasta’)
food( )
Important Rules for Default Parameters
Any parameter cannot have a default value unless all parameters appearing on
its right have default values.
interest ( 3000, 5 )
interest ( 3000 )
Named Arguments
def interest ( prin, time, rate=0.15 ):
:
interest( rate=0.12, time=8, prin=1000 )
interest( time=5, rate=0.15, prin=2500 )
interest( time=5, prin=2500 )
interest( 1000, rate=0.12, time=4 )