Test Index

CBSE Class 12 Computer Science 2026 Paper Compartment Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 23 of 37
Marks: +1, -0
What are default parameters in Python functions ? Explain with the help of an example.
Solution:  
Default parameters:
A default parameter is a parameter that is given a default value in the function header (parameter=value). If the function is called without an argument for it, the default value is used; if an argument is passed, it replaces the default.
Default parameters must come after all non-default parameters in the header, e.g. def f(a, b=5): is valid but def f(a=5, b): is a SyntaxError.
Example:
def greet(name, msg="Good morning"):
print(msg + ",", name)
greet("Anu") # msg takes default value
greet("Ravi", "Hello") # default replaced by "Hello"
Output:
Good morning, Anu
Hello, Ravi
In the first call only name is passed, so msg takes its default value "Good morning". In the second call the passed value "Hello" overrides the default.
© examsnet.com
Advertisements
Go to Question: