Default Parameter Values
Sometimes it is useful to set a default value for a parameter, for example when the parameter value is often repeated.
Default parameter values allow us to call the function without having to provide all parameter values. If a value is not provided for a parameter with a default value, the default value is used. We specify the default value of a parameter during the function definition using the =
operator. For example:
def greet(name="world"):
print("Hello, " + name)
This function has one parameter name
with a default value "world"
. When we call the function without an argument:
greet() # Outputs: Hello, world
The function uses the default value and prints Hello, world
.
When we call the function with an argument:
greet("Karel") # Outputs: Hello, Karel
The function uses the provided value and prints Hello, Karel
.
Instructions
Set the default value of the name
parameter in the welcome
function to "visitor"
.
Start programming for free
5/7