Test Index

Functions in Python Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 17 of 23
Marks: +1, -0
What will be the output of the given code ?
a=10
def convert(b=20):
a=30
c=a+b
print(a,c)
convert(30)
print(a)
[CBSE 2024 Compartment]
Solution:  
Concept:
A variable assigned inside a function is local to that function. It hides a global variable with the same name but does not change it. A value passed as an argument overrides the default value of the parameter.
Explanation:
a=10: global a = 10.
convert(30): b = 30 (the default 20 is not used). Inside, local a = 30, c = 30 + 30 = 60, so it prints 30 60.
print(a): outside the function the global a is still 10.
Output:
30 60
10
© examsnet.com
Advertisements
Go to Question: