python / beginner
Snippet
Default Function Arguments
You can assign default values to function parameters. If no argument is provided during the call, the default value is used.
snippet.py
1
2
3
4
5
def greet(name="Guest"):print("Hello, " + name)greet()greet("Alice")
Breakdown
1
def greet(name="Guest"):
Defines a function with a default parameter 'Guest'.
2
print("Hello, " + name)
Prints a greeting using the provided or default name.
3
greet()
Calls the function without an argument; uses 'Guest'.
4
greet("Alice")
Calls the function with 'Alice', overriding the default.