Concepts / Function Design Best Practices

Function Design Best Practices

Default values make selected parameters optional. The caller can accept the function's standard behavior without supplying that argument, or provide a different value when customization is needed.

  • Programming

A Useful Fallback

A function often has one behavior that is useful in most situations, while still needing to support special cases. For example, a greeting function might normally use the word Hello, but sometimes a caller may want Hi or Greetings. A default argument value lets the function provide the common choice automatically while allowing the caller to replace it when necessary.

Definition

A default argument value is a value assigned to a function parameter that Python uses when the caller does not provide an argument for that parameter.

Key point

Default values make selected parameters optional. The caller can accept the function's standard behavior without supplying that argument, or provide a different value when customization is needed.

Tracing the Argument Choice

Caller argumentprovided valueParameterbound valueFunction bodyuses the parameterDefault valuefallback valueargument providedargument omittedvalue available
How does a value supplied by the caller flow into a parameter, and when does the default value flow in instead?

When Python encounters a function call, it checks the arguments supplied by the caller. If the caller provides a value for a parameter, that value is used. If the caller omits an argument for a parameter with a default, Python uses the default instead. After the parameter is bound to a value, the function body works with that value in the same way, regardless of whether it came from the caller or from the default.

Provided callgreeting = HiHiparameter valueOmitted callno greeting argumentHellodefault valueargument supplieddefault selected
What value does a parameter hold when the function is called with an argument versus when the argument is omitted?

What do you think happens?

A greeting function has a greeting parameter whose default is Hello. What value will greeting hold when the caller omits that argument?

  • Hello

  • The parameter has no value

  • The function chooses a random greeting

Reveal answer
Answer:

Hello

The default value is used when the caller does not provide an argument for that parameter.

Writing the Default

parameter_nameparameter=assignment operatordefault_valuefallbackfollowed byfollowed by
Where is the default value written in a function definition, and which part identifies the parameter as optional?

The general pattern for a default parameter is parameter_name = default_value. This pattern appears inside the parentheses of the function definition. The parameter name identifies which parameter receives the value, the equals sign connects the parameter to its fallback, and the value on the right is used when the caller omits that argument.

Worked example

Choosing a Greeting

Design a greeting function whose greeting parameter uses Hello by default but accepts a different greeting when the caller provides one.

  1. 1
    Choose the common case

    Use Hello as the default because it is the greeting wanted in the usual case.

  2. 2
    Attach the fallback

    Write the parameter and its default in the form greeting = Hello inside the function definition.

  3. 3
    Trace an omitted argument

    If the caller does not supply greeting, the parameter receives Hello.

  4. 4
    Trace a supplied argument

    If the caller supplies Hi, the parameter receives Hi instead of the default.

Result

One function supports both the common greeting and customized greetings without requiring the caller to provide a greeting every time.

Key point

A default does not force the parameter to keep one value. It supplies a value only when that parameter is omitted from the call.

Independent Parameters

Python makes the argument-or-default decision independently for each parameter. A function can therefore receive a caller-provided value for one parameter while using the default for another. Once each parameter has been bound, the function body simply works with the resulting values.

Worked example

Calculating an Order Total

An order-total function uses a tax rate of 0.08 by default, but some callers need to provide a different tax rate.

  1. 1
    Default case

    When the caller supplies an order but omits the tax rate, the tax-rate parameter receives 0.08.

  2. 2
    Special case

    When the caller supplies a different tax rate, that supplied value replaces 0.08 for that call.

  3. 3
    Design result

    The function handles the common tax situation conveniently while remaining flexible for callers with a different rate.

Result

The default tax rate provides sensible behavior for typical calls, and an explicit argument supports specialized calls.

This pattern is useful when a function has a sensible value for most cases but should still allow customization. The default reduces unnecessary input for ordinary use, while the optional argument keeps the function useful in less common situations.

Safe Default Values

Immutable valuesstrings, numbers, tuples,NoneStable fallbackrecommended defaultMutable valueslists, dictionariesPersistentmodificationunexpected behaviorsupportscan cause
Which kinds of values can be assigned as defaults, and how do valid and risky defaults differ?

A default should be a fixed, immutable value. Immutable values cannot be changed after they are created. The source identifies strings, numbers, tuples, and None as safe default values.

Default categoryExamplesDesign guidance
ImmutableStrings, numbers, tuples, NoneSafe choices for defaults
MutableLists, dictionariesAvoid because modifications can persist across calls

The source recommends immutable values as defaults and warns against mutable values.

Common Design Mistakes

  • Common mistake

    Treating a default as a required argument

    What it looks like

    Assuming the caller must always provide the parameter even though the function definition gives it a default.

    Why it fails

    The purpose of a default is to make that parameter optional when the caller does not provide an argument.

    Do this instead

    Let the function use its fallback for the common case, and provide an argument only when a different value is needed.

  • Common mistake

    Expecting the default to override a supplied argument

    What it looks like

    A caller supplies a different greeting but the function is expected to use the standard greeting anyway.

    Why it fails

    Python uses the provided argument when one exists.

    Do this instead

    Remember that the default is used only when the argument is omitted.

  • Common mistake

    Using a mutable value as a default

    What it looks like

    Choosing a list or dictionary as a default and modifying it inside the function.

    Why it fails

    The modification can persist across function calls and produce unexpected behavior.

    Do this instead

    Use an immutable value such as a string, number, tuple, or None.

  • Common mistake

    Assuming every parameter receives its value from the same source

    What it looks like

    Expecting all parameters to use defaults because one argument was omitted.

    Why it fails

    Python decides independently for each parameter.

    Do this instead

    Trace each parameter separately: a supplied argument wins for that parameter, while an omitted argument selects its default.

MEDIUM

A function has a message parameter with the default value Hello and a rate parameter with the default value 0.08. For each call, identify the value held by each parameter: a call that supplies neither argument, a call that supplies only message as Hi, and a call that supplies only rate as 0.05.

Hints
  • For each parameter, ask whether the caller supplied an argument.
  • Use the supplied value when present; otherwise use that parameter's default.
  • Do not let the choice for one parameter determine the choice for the other.
  1. 1
    Default argument values provide fallback behavior for omitted arguments. Write them as parameter_name = default_value in the function definition. A supplied argument replaces the default for that call, and each parameter is evaluated independently. Prefer immutable defaults such as strings, numbers, tuples, and None; avoid mutable defaults such as lists and dictionaries because modifications can persist across calls.

Key Takeaways

  • Default argument values make selected function parameters optional.
  • The syntax is parameter_name = default_value inside the function definition.
  • Python uses a supplied argument when present and the default when the argument is omitted.
  • The choice is made independently for each parameter.
  • Use immutable defaults such as strings, numbers, tuples, and None; avoid mutable lists and dictionaries.

More concepts starting with F