Functions PPT Parul

You might also like

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

FUNCTIONS

Topics covered:
❖ SCOPE of Function Local & Global
❖ Example Programs to explain Local & Global Scope
❖ Global Keyword
❖ Passing Mutable & Immutable arguments to a functions
with Example Programs
❖ Some Board Exam Questions
❖ Flow of Execution
Global Variable is accessible inside as well as outside the
function
What if ,a same
variable is declared
inside as well as
outside the function?
Preference will be given to Local
variable, inside the function
If I want to access a Global
variable within the Local
scope?
Using Global keyword we can
modify the value of a Global
variable within the Local scope
30
20
20
output
B)
Passing Mutable & Immutable Arguments to a Function

Till now we have passed single constant value to


functions either through positional or Keyword
arguments. We can pass multiple values to the function
using lists, tuples ,dictionary.
1. When integers, floats, string or tuples are passed to the functions as parameters,
that is said to be Immutable Argument. Then, the changes made to these
parameters are not reflected back in their respective arguments.

1. Here, Changes made in


n are not reflected
back in a ,since it is
integer and immutable

2. Same will be the case


for other immutable data
types like string & tuples
We can also pass a tuple (immutable) as an argument to a function. Then, a function
can only access the value of a tuple ,but cannot modify it.

Explanation
In this program, tuple, tup is passed
as an argument to the function
func(). In order to perform the
mathematical processing ,it is
required to convert into a list using
the method list(). The next two
statements alter the value of list
and hence the changes are reflected
when these values are displayed.

But, outside the function the values


of tuple are not modified and
therefore remain same as 100 & 200
When string (immutable) is passed as an argument to the function, then function
can access the value of the string but cannot modify it.
If we want to modify the string, the solution is to create another string and
concatenate the modified value of the parameter string in the newly created string.
This is explained with the following Example :-
2. When List or dictionary is passed as an argument, the changes are reflected back,
after the function calls.

❖ Changes
made to
the list are
reflected
back after
the
function
calls.
Q1. WAF lenWords(STRING), that takes a string as an argument and returns
a tuple containig length of each word of a string. For eg, if the string is :
“Come let us have some fun”, the tuple will have (4,3,2,4,4,3)
Q2. WAF CountNow(Places) in Python, that takes the dictionary
Places as an argument and displays the names in uppercase of the
places whose names are longer than 5 characters.
Q3. WAF index_List(L) where L the list of elements passed as
argument to the function. The function returns another list
named ‘index list’ that stores the indices of all non-zero
elements of L
1 def greatest(a,b,c):
6 if a>b and a>c:
7. return a FLOW OF
8. elif b>a and b>c: EXECUTION
9. return c
10. else :
11. return c
2. x= int(input(“enter first number”))
3. x= int(input(“enter first number”))
4. x= int(input(“enter first number”)
5. res = greatest(x,y,z)
12. print(res)

You might also like