0Pricing
Mojo Academy · Lesson

Parameterized Functions

Specialize a function with [bracket] parameters.

Parameterized Functions 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.

Functions With a Parameter

A parameterized function takes a compile-time value in square brackets, letting one definition adapt itself before the program runs. ⚙️

fn repeat[count: Int](msg: String):
    for i in range(count):
        print(msg)

The Bracket List

Place your parameters in brackets right after the function name, then your normal arguments in parentheses just after that.

fn scale[factor: Int](x: Int) -> Int:
    return x * factor

Calling With a Parameter

At the call site you fill the bracket with a constant, then pass runtime arguments in parentheses as usual.

var y = scale[10](4)

Each Value Makes a Version

Mojo builds a fresh specialized copy of the function for each parameter value you use, each one tuned exactly for that constant.

Type Parameters

A parameter can even be a type. This is how you write one function that works for Int, Float, or any type you choose.

fn first[T: AnyType](value: T) -> T:
    return value

Generic With Traits

Constrain a type parameter with a trait so the body can safely use the methods that trait promises every conforming type has.

fn show[T: Stringable](value: T):
    print(String(value))

The Compiler Inlines It

Because the parameter is a constant, the compiler can fold it directly into the code, often removing loops or branches entirely.

Mixing Parameters and Arguments

One function can take both: a compile-time parameter for structure and runtime arguments for the actual data it processes.

fn fill[n: Int](value: Int):
    for i in range(n):
        print(value)

Inferred Parameters

Sometimes Mojo can infer a parameter from the arguments, so you write less and the compiler fills in the rest for you.

Why It Stays Fast

Each specialization is its own optimized function with no runtime cost for the parameter, which is the heart of Mojo's speed.

One Source, Many Builds

You maintain a single readable definition, and the compiler quietly produces many fast, specialized builds behind the scenes.

Quick Check

Recall how a parameterized function is written.

Recap

You wrote parameterized functions: brackets hold compile-time values, the compiler specializes each version, and one source becomes many fast builds. 🎯

Frequently asked questions

Is the “Parameterized Functions” lesson free?

Yes — the full text of “Parameterized Functions” 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 “Parameterized Functions”?

Specialize a function with [bracket] parameters. 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 “Parameterized Functions” 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. Parameters vs Arguments
  2. Parameterized Functions
  3. Parameterized Structs
  4. Why Compile-Time Wins Speed
← Back to Mojo Academy