0Pricing
Mojo Academy · Lesson

Positional and Keyword Arguments

Send values by position or by name.

Positional and Keyword Arguments is a free Mojo Academy lesson on CoddyKit — lesson 1 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.

Two Ways to Pass Values

Every Mojo function call sends in values called arguments. You can send them by position or by name, and both styles can live in one call.

Positional Arguments

With positional arguments, order is everything. The first value fills the first parameter, the second fills the second, and so on.

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

greet("Mojo", 2)

Order Matters Here

Swap two positional arguments and the meaning changes. Here Mojo would read 2 as the name and "Mojo" as the count, so order must match the parameters.

greet(2, "Mojo")  # wrong: positions no longer match

Keyword Arguments

A keyword argument names the parameter you mean. Now the call reads clearly and order stops mattering.

greet(name="Mojo", times=2)

Keywords Free You from Order

Because each value is labeled, you can list keyword arguments in any order. Mojo matches them by name, not by position.

greet(times=2, name="Mojo")  # same result

Mixing Both Styles

You can combine the two, but positional values must come first. Once a keyword appears, everything after it must also be a keyword.

greet("Mojo", times=2)  # ok
# greet(name="Mojo", 2)  # error

Why Keywords Help Readers

Keyword arguments turn a mystery call into a sentence. Reading times=2 tells you exactly what the 2 does, even months later.

Avoiding Position Bugs

When a function takes several same-typed values, positions are easy to mix up. Naming them with keywords removes that whole class of bug.

draw(width=800, height=600)  # clearer than draw(800, 600)

Names Come from Parameters

The keyword you type must match the parameter name in the function definition. A typo like nme= is an error, not a new argument.

fn greet(name: String, times: Int): ...
greet(name="Mojo", times=2)

Both Styles, One Function

You do not pick a style per function. The caller decides, so the same function accepts positional, keyword, or a mix on each call.

A Practical Rule of Thumb

Use positional for the obvious leading value, then switch to keywords for extras. This keeps short calls tidy and long calls clear.

connect("localhost", port=8080, retries=3)

Quick Check

You want order to stop mattering in a function call. Which approach helps?

Recap: By Position or Name

Positional arguments rely on order; keyword arguments rely on names. Lead with positions, label the rest, and your calls stay both short and clear.

Frequently asked questions

Is the “Positional and Keyword Arguments” lesson free?

Yes — the full text of “Positional and Keyword Arguments” 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 “Positional and Keyword Arguments”?

Send values by position or by name. 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 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Positional and Keyword Arguments” 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