0% found this document useful (0 votes)
41 views6 pages

DocScanner 8 Dec 2023 2-00 PM

1. The document discusses memory environments for passing mutable types like lists to functions. 2. When a mutable type like a list is passed as a parameter to a function, the function receives a reference to the original list rather than a copy. 3. Any changes made to the list within the function are reflected in the calling environment after the function returns because the list parameter is still referring to the original memory location.

Uploaded by

sheetal10sweta
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)
41 views6 pages

DocScanner 8 Dec 2023 2-00 PM

1. The document discusses memory environments for passing mutable types like lists to functions. 2. When a mutable type like a list is passed as a parameter to a function, the function receives a reference to the original list rather than a copy. 3. Any changes made to the list within the function are reflected in the calling environment after the function returns because the list parameter is still referring to the original memory location.

Uploaded by

sheetal10sweta
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/ 6

FUNCTIONS

WITH
WORKING
Chopter 3: Sample Code 2.2 (Contd....)
Environment For
Memory
38100
(1) 364 380 0
(2) 348
332
316
4 5
2 3 At line7, statement
myList.remove(s) removes element
5 from list and thus only three
6-L Global Environment elements are left in List(myList).
list
sau But the memory address of the
List1 Local Environment the same even
(myFunc3( )) (myList) remains
myList after changes in its contents.
same
The environment remains the
till line 9 and then control
returns to
line13.

olh (2) 38100


Li 316 (1) 0 1 2

3 4 5 6
1 2
Sams

At line 13, after returning from


myFunc3() the local environment
Global Environment still
is removed. The list. List1 is
List1 referencing the address 38100
with all the changes intact.

Sample Code 2.3

Cest

a
Passing a Mutable Type Value to function
- Assigning parameter to a new valuelvariable.
:
1. def
my
List)
Func4 (my
now")
2. print ("\n\t Inside CALLED Function
3. print (" \t List received: ", myList)
new =
[3,5] a new value (a list here)
Parameter list get assigned
5. myList = new

6. myList. append (6)


changes:", myList)
7. print("\t List within called function, after
8. return

9. List1 = [1, 4]
10. print ("List before function call :",
List1)
11. myFunc4( List1)
L2. print ("\nList after function call
:", Listl1)
COMPUTER SCIENCE

above Code
as
shown below: Chapter 3:
produced by
outpul Mernory
130
carefully look at the
Now [1, 4] (1)

function call: The value


go crare
532
before now 135 6]iside
List
CALLED
Function changes:([3, 5, 61) DID NOT
furirn
GET PEFLESTE 1
Inside 4] after
received: [1, function,
List within called
4
List
4] Line

function call :[1,


List after
mutable type, a list. Right ?
But stills
of a
passed value is Why ? No Worries.
unexpected, the reflected back to_main_. Le
Isn't this do not get created for above code.
function memory environments
made in the
a look at the
But first have

Memory Environment For Sample Code 2.3


5

(Note Passed value is a list,


: a mutable type) Line

(1) (1)
532 548 564 580 596 612 40116

9-10 1 2 3 4 6
1 of list referD
Lines Elements 0 and they hod
addresses of values

Global Environment
List1 9.10ofat
Till Lines 6-8
main
of Lines

(1)
532 (1)
580

1-3
12 3 4 5 6
40116

11,
Lines

nct 12
line10 agu Line

List1 At ed
COMPUTER SCIENCE WITH

the moment you make the


that parame
above code name, it losoc the address of
environsof variable will not
132 memory a diferent made to it post this get retlec
from the
by
assigning are
changes because the addresses became different
isclear address whatever
As it another : S
referto passed to it So was mutable
summarized
beloW
eventhoughit
behaviour as
angument P
original
argument
mutable parameter (say
summarize the mutable
can assigned to
Mutoble
Argument (say A)
does not
change
Changesif if

if P is assigned a new name or differert


assigned any
new name or data type value (e.g., P= diferent_vanatle
nof
IPs
diferent data type
value then changes then any changes in Pare not refiecter
GET
in place and BACK to main
in P are performed
REFLECTED BACK to main (refer sample code 2.3)
same codes 2.1 and 22).
(refer That is, A will not show changed data
That is, A will show the changed data.

Figure 3.5 Mutable types' behaviour with functions.

That means, we can say


:

ChodDoint that
3.1 > Changes in immutable types are not reflected in t

caller function at all.


f return statement is not used inside
he function, the function will > Changes, if any, in
mutable types
return:
)0 (b) None object are reflected in caller
) an arbitary
function if its name s
intege assigneda different variable or datatype.
) Error! Functions in are not
Python must
have a return statement. reflected in the caller function
ich of the following assigned a different variable or datatype.
keywords marks
beginning of
the function block?
func (b) define
(c) def (d) function
t is
the area of memory
-h stores called,
NoTE
the parameters reflected
bles of a and local Changes in the immutable types are not
function mutatk
call ? the in the
heap function at all. And changes
(b) storage area only ift
stack (d) an array
eflected in the cale stion,changed!
he errors type of the va t
in following
ions : function nction.
ef mair
FUNCTIONS
WORKING WITH
Chopter 3 137
bles Problem
Solved
of having functions in a program ?
What is the significance
eler 1.
functions in programs is very useful. It offersfollowing
in Solution. Creating advantages:
to understand.
() The program s easier
ain block of program becomes cOmpact as the code of functions is not part of it, thus is
easier to read and understand.
Redundant code is at one place, so making changes is easier.
(i)
cion lnstead of writing code again when we need to use it more
than once, we can write the
he form of a function and call it more than once. If we later need to change the code,code
we
change it in one place only. Thus it saves our time also.
(iD Reusable functions can be put in a library in modules.
We can store the reusable functions in the form of modules. These modules can
be imported
and used when needed in other programs.
2 From the program code given below, identify the parts mentioned below :

:
def processNumber (x)
=
X
72
return x +3
y= 54
res = processNumber (y)
Identify these parts : function header, function call, arguments, parameters, function body, main program

Solution.
:
Function header def processNumber (x)
Function call processNumber (y)
Arguments y
Parameters X

Function body X = 72
return x + 3

y = 54
Main program
res = processNumber (y)
ant : [CBSE 2020C)
J. Find and write the output of the following python code

te def ChangeVal (M, N) :


tha for i in range(N):
== 0 :

if M[i]%5
M[i]//= 5
== 0 :

if M[i]%3
M[i]//= 3
L=[ 25,8,75, 12]
ChangeVal (L,4)
for i in L:
print (i, end ='#')
Solution. 5#8#5#4#
are called function variables
136 function header
parentheses of a
vithin the statement of_main segment.
4. Variables
that are listed begins with first
program execution
the function call. prOvided for that parameter 1:
5. In Pvthon skipped in
parameters cannot be
only if no value is
6 Detault parameters are
considered in
values for
7 The default
call statement.
the tunction values.
may return multiple
pvthon tunction to its caller.
8A returns a value ie., None
void function
A
also scope.
9
functions can have global
inside
10 Variables defined
name as that of global
a variable, hides the global variable in itsfuncion
having the same
11 A
lxal variable
AsSERTIONS AND REASONS

DIRECTIONS
(A) is followed by a statement of Reason (R)
In the folloeing questions, a statement of Assertion
Mark the correct choice as
:

(a) Both A and R are true and R is the correct explanation of


A. 2
A.
(b) Both A and R are true but R is not the correct explanation of
(0A is true but R is false (or partly true).
() A is false (or partly true) but R is true. (e) Both A and R are false or not fully true
1 Assertion A function is a subprogram.
Reason. A function exists within a program
and works within it when called.
2 Assertion. Non-default arguments cannot
follow default arguments in a function call.
Reason. A function call can have different types
of arguments.
3 Assertion.
parameter having a default in
A
function header becomes optional in function
Reason. A function call may or call.
may not have values for default arguments.
4 Assertion. A variable
declared inside a function
Reason. variable cannot be used
A
created inside a function outside it.
5. has a function scope.
Assertion. A variable
not
declared at a higher scope declared inside a function can still
level. be used inside the function
Reason Python if it s
resolves a name
Built-in scopes, in using LEGB rule where it checks
the given order. in Local, Enclosing, Global a
6.
Assertion. A parameter
having a default 3
Reason. The value in the function
default values for header is known as
parameter in the parameters are considered a default paranele
function call statement. only if no value
7.
Assertion. A
function declaring a is provided fot
that global variable. variable having
Reason. A the same name as a
local variable global variable, cannot us
havingthe same name
8.
Assertion. as a global variable
as defined If the arguments in a function hides the
in the function call statement global variable in its function.
definition, match
Reasoning. During a such arguments are the number arguments
positional argument(s).
function ca
call, the argument called positionaland order of
list first contains arguments.
NOTE : default argument(s) followed by

Answers for 0T0s are


given at the (CBSE Sample Paper 2)
end of the book.
FUNCTIONS
WORKING WITH
ON
-X Chopter 3 135
variable defined outside all the functions referred to as ?
a
What is variable (b) A global variable (c) A
13
static local variable
(a) A defined (d) An automatic
variable inside a
function referred to as variable
a
ndlocal What is A
global variable (c) local variable
static variable () A

(a) A (d) An autormatic


Carefully observe the code and give the answer. variable

def function1 (a):


15
nippet.
a
=a+'1'
a=a2
>>>function1 ("hello")
indentation Error (b) cannot perform mathematical
(a) operation on strings
(d) hello2 hello2
(c) hello2
code 2
K
What is the result of this
def print_double(x):
print (2 ** x)
print_double (3)
(a) 8 (b) 6 (c) 4 (d) 10

r What is the order of resolving scope of a name in a Python program ?

(L: Local namespace, E Enclosing namespace, B: Built-In Namespace, G: Global namespace)


:

Python (a) B
GEL (b) L E G B (c) GEBL (d) L BEG
call a ?
(Term 1 18. Which of the given argument types can be skipped from function
(b) keyword arguments
(a) positional arguments
(d) default arguments
(c) named arguments
FILL IN THE BLANKS

often returns a value.


1. A subprogram that acts on data and
1S a
as
2 Python names the top level segment (main program)
segment.
program execution begins with first statement of
In Python, are called
through a function-call statement
passed
e
values being
DIhe values recejved in the function
definition/header are called as a
function header is known
(Tertn 1 6. A
parameter having default value
in the statement.
7.A can skipped in the function call passed in the function
call
n ? argument be values being
8
named arguments with assigned
guments are the
statement.
caller.
3.
A void function also returns a value to its statements (main
program) as
run.
10. By default, segment with top-level executed during a program
inition? Python names the statements are
11. The
order in which
12. The
reters to the defined in
function
default value for a parameter is
QUESTIONS definition.
TRUE/FALSE
argument in a function
parameter.
or after a detault
before as a default
1.
Non-default placed known is also
(Term1) drguments can be function header is ends with a colon (:),
the def and
parameter having default value in
2.
with keyword
A
begins
3.
Thefirrst line of function definitionthat
Known as function header.

You might also like