0Pricing
Mojo Academy · Lesson

Writing a def Function

Python-style, dynamic and forgiving.

Writing a def Function 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.

Functions Bundle Logic

A function packages a piece of work under a name so you can run it whenever you like, instead of repeating code. 🧩

Meet the def Keyword

The def keyword defines a function the Python way: relaxed about types and easy to write while you explore an idea.

def greet():
    print("Hello from Mojo")

Calling Your Function

Defining a function does not run it. You call it by writing its name followed by parentheses, and the body executes.

greet()

Passing in Arguments

Put parameters in the parentheses to feed data in. Here name becomes a value you can use inside the body.

def greet(name):
    print("Hi", name)

Types Are Optional in def

In a def you may skip type annotations entirely. Mojo lets the value flow dynamically, just like Python does.

def double(x):
    return x * 2

Returning a Value

The return keyword hands a result back to whoever called the function, ending the function right there.

def add(a, b):
    return a + b

Arguments Are Mutable Copies

Inside a def, arguments act like local copies you can reassign freely without touching the caller value.

def bump(x):
    x = x + 1
    return x

def Feels Like Python

If you know Python, def is instantly familiar: same syntax, same forgiving feel, now running on Mojo.

Great for Quick Experiments

Because it asks for so little, def shines when prototyping or scripting, where speed of writing matters most.

Indentation Defines the Body

Everything indented under the def line is the function body. Mojo, like Python, uses indentation to group code.

def info():
    print("line one")
    print("line two")

Docstrings Document Intent

A string on the first body line is a docstring: a short note describing what the function does for future readers.

def area(r):
    "Return circle area."
    return 3.14 * r * r

Quick Check

Think about how def handles type annotations.

Recap

You met def: the Python-style function in Mojo. Types are optional, arguments are mutable copies, and it is perfect for quick, forgiving code. 🎯

Frequently asked questions

Is the “Writing a def Function” lesson free?

Yes — the full text of “Writing a def Function” 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 “Writing a def Function”?

Python-style, dynamic and forgiving. 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 “Writing a def Function” 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. Writing a def Function
  2. Writing an fn Function
  3. When to Reach for Each
  4. Return Types and None
← Back to Mojo Academy