2 - 3 Functions 1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40

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

You might also like