Make Function Calls Clear

Format a returned mean with a common default unit, then use positional and keyword arguments to make each call's choices visible.

The mean function needs one input, so a positional call is easy to read:

value = mean([18, 21, 20, 23])

Formatting the result introduces a choice. Most reports use degrees Celsius, but another report may need a different unit.

Give a Common Choice a Default

value is required. The parameter unit has the default value "°C". When a call omits the second argument, Python uses that default:

A default should represent a genuine common case. It should not hide a value that most callers need to choose.

Q1. Use the default unit

What does format_mean(19.24) return?

Choose one

Select one choice, then check.

HintLook at the omitted argument

The call supplies value but does not replace unit.

SolutionThe default supplies °C

The value rounds to one decimal place, and the omitted unit remains "°C".

Not attempted
Review

Not marked done.

Name an Argument When the Name Helps

Arguments normally bind to parameters from left to right. These two calls are equivalent:

The second call contains one positional argument and one keyword argument. Writing unit= makes the role of "°F" visible. This is useful when a value's meaning would otherwise be unclear or when the call replaces a default.

Positional arguments must appear before keyword arguments:

format_mean(68.9, unit="°F")

Writing format_mean(unit="°F", 68.9) is invalid because an unnamed positional argument follows a keyword argument.

A required parameter can also receive a keyword argument:

format_mean(value=68.9, unit="°F")

Q2. Choose the clearest valid call

Which call supplies a value of 68.9 and replaces the unit with °F?

Choose one

Select one choice, then check.

HintSupply both roles

value is required; unit may be named when the call replaces it.

SolutionUse one positional and one keyword argument

format_mean(68.9, unit="°F") supplies the required value and clearly names the optional choice.

Not attempted
Review

Not marked done.

Put Required Parameters First

In a definition, a parameter without a default must appear before an ordinary defaulted parameter:

The definition therefore begins def format_mean(value, unit="°C"):. The complete function body remains the one used above.

This order lets Python decide which argument binds to which parameter. Keep data flow visible: the caller provides a value, may choose a unit, and receives one string. The function does not reach into an outside name for either input.

Q3. Complete a useful default

Complete the function so the ordinary call displays Mean: 20.5 °C and a second call can request another unit.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintWrite the common value in the definition

Change the second parameter to unit="°C".

SolutionDefault the unit
Not attempted
Review

Not marked done.

Defaults cover common choices; keyword arguments make selected roles visible. Clear calls expose the function’s inputs. Next we will check whether the function returns the intended result for ordinary and boundary cases.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.