0% found this document useful (0 votes)
52 views24 pages

Functions

The document discusses Python functions including defining functions with parameters and return values, different types of functions, scope of variables, passing immutable and mutable objects to functions, and the LEGB rule for name resolution. It provides examples of defining functions with different argument types like positional, default, and keyword arguments. The document also explains how changes made to immutable and mutable objects inside functions are reflected back in the calling function.

Uploaded by

Sowiuh Sledge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views24 pages

Functions

The document discusses Python functions including defining functions with parameters and return values, different types of functions, scope of variables, passing immutable and mutable objects to functions, and the LEGB rule for name resolution. It provides examples of defining functions with different argument types like positional, default, and keyword arguments. The document also explains how changes made to immutable and mutable objects inside functions are reflected back in the calling function.

Uploaded by

Sowiuh Sledge
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Functions: Using Python

Sarman Singh
Seth Anandram Jaipuria School
[email protected]

Sarman Singh 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

Functions 2/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.

Functions 3/22
Syntax for functions

def function_name(parameters):
"""docstring""" statement(s)
return [expression]

Functions 4/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

Functions 5/22
Example: Function

f(x) a mathematical function

f (x) = x2
Above mentioned mathematical function can be written in
Python like this:

def f(x):
return x*x

Functions 6/22
Anatomy of Python function

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

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
value(s) in function call → Arguments/Actual
Arguments

Functions 9/22
Passing parameters

1 Positional arguments (Required arguments)


2 Default arguments

3 Keyword (or named ) arguments

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

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

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)

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)

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)

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)

Functions 14/22
Returning values from function

Functions returning some values (non-void


functions)
Functions not returning any value(void functions)
Functions returning multiple values

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 havelocal
scope, i.e. it can be used only within this function.

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

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

Functions 18/22
Name Resolution (LEGB Rule)

Local Enclosing Global Built-in

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.
Depending upon the mutability/immutability of its data
type, a variable behaves differently.

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 changethe memory
address of the variable.

Functions 21/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.

Functions 22/22

You might also like