## What Default parameters allow [[parameter|parameters]] to be [[initialise|initialised]] with default values if no value is provided in the [[subprogram]] call. The default values of default parameters are specified in the subprogram [[signature]]. ## Syntax A default value is assigned directly in the subprogram signature, using `=`, e.g., `def greet(name, greeting="hello"):`. When an overriding value isn't provided in the subprogram call, the parameter takes its default value; default values can be overridden when the subprogram is called. You cannot place required (positional) parameters after parameters with default values—so default parameters must be the last parameters listed in a subprogram's signature. ## Why Default parameters are never mandatory—their inclusion is a design choice that signals the normal behaviour of a subprogram, so they should be used intentionally and only when a sensible default value exists. There's no benefit to a default that gets overridden constantly. The main benefit is reduced subprogram call length and repetition: rather than repeatedly passing the same value, you write it once in the signature, and callers only need to supply a value when it should differ from the default. > [!WARNING] Risk of reduced readability > Using default parameters potentially reduces your code's readability, since calls become less explicit about which values are being set—unless you use [[Keyword arguments|keyword arguments]], shown in the [[Default parameters#Call subprograms with multiple defaults|example below]]. ## Examples ### Subprogram with sensible default value The default value represents the most common use of the subprogram. ```python def greet(name, greeting="Hello"): # greeting has the default value "Hello" print(f"{greeting}, {name}!") greet("Alice") # "Hello, Alice!" greet("Bob", "Hi there") # "Hi there, Bob!" ``` ### Subprogram with sensible Boolean flag default value The default value controls optional behaviour rather than storing required data; such [[Boolean data type|Boolean]] parameters are often called flags. ```python def format_name(first, last, uppercase=False): # uppercase has the default value False name = f"{first} {last}" if uppercase: name = name.upper() return name name = format_name("Ada", "Lovelace") # Ada Lovelace upper_name = format_name("Ada", "Lovelace", uppercase=True) # ADA LOVELACE ``` > [!warning] Single-responsibility principle > Be wary if you find yourself writing a subprogram with lots of Boolean flags that control the subprogram's behaviour as it likely breaches the [[Single-responsibility principle|single-responsibility principle]]. ### Constructor with sensible default The default value in the [[constructor]] signature sets the initial [[object state|state]] of the [[object]] when no explicit value is provided. ```python class BankAccount: def __init__(self, account_holder, balance=0): # balance has the default value 0 self.account_holder = account_holder self.balance = balance a = BankAccount("Herman") b = BankAccount("Farah", 1000) print(a.balance) # 0 print(b.balance) # 1000 ``` ### Call subprograms with multiple defaults Although default parameters can be passed positionally, it's generally clearer to use [[Keyword arguments|keyword arguments]]—especially once a subprogram has more than one default parameter. Keyword arguments let you ignore the parameters whose defaults you want to keep and make your code more readable. ```python def greet(name, greeting="Hello", uppercase=False): greeting = f"{greeting}, {name}!" if uppercase: print(greeting.upper()) else: print(greeting) greet("Sim", "Aloha", True) # "ALOHA, SIM!" greet("Shirley", uppercase=True) # "HELLO, SHIRLEY!" ```