Concepts / Function Design Best Practices

Function Design Best Practices

Default argument values make certain function parameters optional by specifying a value to use if the caller does not provide one.

  • 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.

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.

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

argument providedargument omittedvalue availableCaller argumentprovided valueParameterbound valueFunction bodyuses the parameterDefault valuefallback value
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.

argument supplieddefault selectedProvided callgreeting = HiHiparameter valueOmitted callno greeting argumentHellodefault value
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

followed byfollowed byparameter_nameparameter=assignment operatordefault_valuefallback
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.

Choosing a Greeting

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

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

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

Trace an omitted argument: If the caller does not supply greeting, the parameter receives Hello.

Trace a supplied argument: If the caller supplies Hi, the parameter receives Hi instead of the default.

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

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.

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.

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

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

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

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

supportscan causeImmutable valuesstrings, numbers, tuples,NoneStable fallbackrecommended defaultMutable valueslists, dictionariesPersistentmodificationunexpected behavior
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

Common Design Mistakes

  • Treating a default as a required argument

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

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

  • Expecting the default to override a supplied argument

    Python uses the provided argument when one exists.

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

  • Using a mutable value as a default

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

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

  • Assuming every parameter receives its value from the same source

    Python decides independently for each parameter.

    Fix: 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. 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.