Q. Consider following program code.


1. def myFunc (l, v)

2. l[0] +=2

3. V +=2

4. print("Values/variables within called function :”, l , v)

5. return

6. #__main__

7. list1 = [1]

8. var1 = 1

9. print("Values/variables before function call:", list1, var1)

10. myFunc(list1, var1)

11. print("Values/variables after function call:", list1, var1)


(i) Identify flow of execution in the above program.
(ii) What will be the output of above program?
(iii) What is reason of this output?


Answer :-


(i) 1 -> 7 -> 8 -> 9 -> 10 -> 1 ->2 ->3 -> 4 -> 5 -> 10 -> 11

(ii)

Output :-


Values/variables before function call: [1] 1
Values/variables within called function : [3] 3
Values/variables after function call: [3] 1

>>>


(iii) list1 = [1]

var1 = 1

These are global variable in global environments.

So, 1st output is [1] 1

But If variable and list pass in function then they will in local scope.

So, 2nd Output is [3] 3

After that var1 value again become 1 because variable is in global environments. But list1 value is same because list have no effect of changing environments.

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post