Default Arguments

Carson West

Function Overloading

Default Arguments

Default arguments allow you to specify a default value for a function parameter. If the caller doesn’t provide a value for that parameter, the default value is used.

def greet(name, greeting="Hello"):
  print(f"{greeting}, {name}!")

greet("Alice")  # Output: Hello, Alice!
greet("Bob", "Good morning")  # Output: Good morning, Bob!

Important Considerations:

# Correct
def func(a, b=2):
    pass

# Incorrect - will raise a SyntaxError
def func(a=1, b):
    pass
def process_data(data=None):
  if data is None:
      data = 1 #Create a new list if none is provided
  # ... process data ...

Related Notes: