0Pricing
Mojo Academy · Lesson

Default Argument Values

Make some parameters optional.

Default Argument Values is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Making a Parameter Optional

A default value lets a parameter stand in for itself. If the caller skips it, Mojo quietly uses the value you wrote in the definition.

fn greet(name: String, times: Int = 1):
    for _ in range(times):
        print(name)

Calling With and Without

Now both calls work. Omit times and you get the default of 1; pass it and you override the default for that one call.

greet("Mojo")        # times = 1
greet("Mojo", 3)     # times = 3

Why Defaults Are Handy

Defaults capture the common case. Most callers never touch the option, yet the power is there for the few who need it.

Defaults Go Last

Parameters with defaults must come after required ones. Otherwise Mojo could not tell which value you meant to skip.

fn open(path: String, mode: String = "r"): ...  # ok

A Required-Then-Optional Shape

Think of it as required-first, optional-last. Reading left to right, you supply what is needed, then accept defaults for the rest. This shape is predictable.

Overriding Just One Default

To change a later default while keeping earlier ones, use a keyword. You skip straight to the option you care about.

fn make(width: Int = 80, height: Int = 24): ...
make(height=40)  # width stays 80

Multiple Defaults Together

A function can give several parameters defaults at once. Callers fill only what they want and let the rest fall back.

fn box(w: Int = 10, h: Int = 10, fill: String = "x"): ...

Defaults Build Friendly APIs

Good defaults make a function easy to start with and powerful to grow into. Beginners call it plainly; experts tune the options.

Choosing a Sensible Default

Pick the value most callers would want anyway. A surprising default is worse than none, because it hides behavior the reader did not expect.

The Default Is Evaluated Once

The default expression belongs to the signature, not the body. Treat it as a fixed fallback that documents the normal value.

fn retry(count: Int = 3): ...  # 3 is the documented norm

Defaults Plus Keywords Shine

Defaults and keyword calls are a great pair. Together they let you write functions with many knobs that still feel simple to call.

request(url, timeout=30, retries=5)

Quick Check

Where must a parameter with a default value be placed in a Mojo function?

Recap: Smart Fallbacks

Default values make parameters optional and capture the common case. Put them last, choose them wisely, and override single ones with keywords.

Frequently asked questions

Is the “Default Argument Values” lesson free?

Yes — the full text of “Default Argument Values” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Default Argument Values”?

Make some parameters optional. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Default Argument Values” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Positional and Keyword Arguments
  2. Default Argument Values
  3. Returning Multiple Values
  4. Reading Function Signatures
← Back to Mojo Academy