Lesson 17 of 30

In progress

Course progress

Python

Parameters, defaults and multiple returns

Use positional and keyword arguments, set safe defaults, return several values at once, and avoid the mutable default trap.

A function with two parameters has four ways to be called wrongly. Defaults and keyword arguments remove most of that ambiguity, if you know which ones are safe.

What you will learn

  • Pass arguments by position and by name.
  • Set defaults that do not change between calls.
  • Return several values and exit early.

Positional and keyword arguments

Arguments passed in order are positional. Arguments passed as name=value are keyword arguments, and they can appear in any order.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Keyword arguments make the call site self-documenting, which matters when several parameters share a type. describe("Ana", "tutor", 3) requires the reader to remember what 3 means; level=3 does not.

Rules worth knowing: positional arguments come before keyword arguments, and a parameter cannot receive two values.

describe("Ana", name="Ravi")   # TypeError: multiple values for 'name'

That error is Python protecting you from an ambiguous call, not being difficult.

Defaults belong at the end

A parameter with a default can be omitted. Parameters without defaults must come first, because Python fills positional arguments left to right.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Defaults make the common case short without removing the ability to be specific. Choose defaults that are correct for most callers, and document anything surprising.

The mutable default trap

Defaults are evaluated once, when the function is defined, not on every call. A list or dictionary default is therefore shared across calls.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

The second call prints both fruits, because basket is the same list both times. The fix is a sentinel of None and a fresh container inside the function.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Use this pattern for lists, dictionaries and sets. Numbers, strings and tuples are immutable, so they are safe as defaults.

Returning several values

return a, b packs the values into a tuple, and the caller can unpack them.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Return a tuple when the values belong together and their order is obvious. If a caller would need the docstring to remember which is which, return a dictionary with named keys instead. Three unnamed values is about the limit before a small record becomes clearer.

Early returns keep the happy path flat

A function can return as soon as it knows the answer, which avoids nesting.

Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

Each guard handles one case and gets out. The alternative nests every condition inside the previous else, and the reader has to track four levels of indentation to reach the default.

Deciding what a function returns when it cannot do its job is part of the design. Returning None says "no result", and every caller must check. Raising an exception says "this call failed", and callers that do not check get a traceback instead of a confusing TypeError three lines later. Use None for an optional lookup, and raise for a broken precondition.

Returning None implicitly and then using the result is a common source of TypeError. A function with no return gives None. If callers need a value, return one explicitly on every path, including the error path, or raise instead of returning a quiet None.

Write format_price(amount, currency="INR", decimals=2) that returns a string with the currency first and the amount formatted to the given number of decimals. Then write split_bill(total, people=2) that returns each person’s share rounded to two decimals and the remainder, so the parts add back to the total.

Show solution
Code
Code runs on the Java or .NET runtime, not in a browser tab. Copy this example into your editor and run it there.



Press Run to see the output.

share is the rounded amount each person pays, and remainder is what is left over after multiplying it back. Returning both keeps the caller from guessing which value is which.

Recap

  • Positional arguments fill left to right; keyword arguments name what they set.
  • Defaults are evaluated once, so never use a list or dictionary as one.
  • Returning a tuple is fine for two or three obvious values; use named keys beyond that.

Next, you will see where names live: local variables, globals, and why a function cannot always see what you expect.

Can I force callers to use keyword arguments?
Yes. Put a bare `*` in the signature: `def move(*, x, y):` makes both keyword-only. It is useful for functions with several same-typed parameters where positional order is easy to confuse.
What does *args mean?
It collects extra positional arguments into a tuple, so the function can accept any number of them. `**kwargs` does the same for keyword arguments with a dictionary. Use them for wrappers and flexible APIs, not as a way to avoid naming parameters.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.