Adding Methods to a Struct
Give your type behavior.
Adding Methods to a Struct is a free Mojo Academy lesson on CoddyKit — lesson 3 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.
Data Plus Behavior
So far your struct only holds data. A method is a function that lives inside the struct and gives your type behavior. 🚀
Methods Live Inside
You write a method by defining a function indented inside the struct, right alongside its fields and __init__.
struct Point:
var x: Int
var y: Int
fn show(self):
print(self.x, self.y)self Comes First
A method's first parameter is self, the instance it was called on. Through self the method reads and uses the struct's own fields.
fn show(self):
print(self.x)Calling a Method
You call a method with dot syntax on an instance. Mojo passes that instance in as self automatically for you.
var p = Point(3, 4)
p.show()Methods Can Return Values
Like any function, a method can return a result. Declare the return type after an arrow and hand back a value.
fn sum(self) -> Int:
return self.x + self.yMethods Take Arguments Too
After self, a method can accept more arguments, just like a normal function, to work with extra data the caller provides.
fn scaled(self, factor: Int) -> Int:
return self.x * factorRead-Only by Default
A plain self parameter is borrowed, so the method can read fields but not change them. This keeps your data safe.
Mutating with mut self
To let a method change the instance, mark the parameter mut self. Now assignments to self.x actually update the value.
fn move_right(mut self, step: Int):
self.x += stepMethods Build on Each Other
One method can call another through self, so you compose small behaviors into larger ones without repeating code.
fn report(self):
print(self.sum())Encapsulation in Action
Keeping fields and the methods that use them in one struct is encapsulation. The type owns both its data and how that data behaves.
A Struct That Does Work
Here a Point both stores coordinates and reports their sum. Your type is now active, not just a passive data container.
struct Point:
var x: Int
var y: Int
fn sum(self) -> Int:
return self.x + self.yQuick Check
Let us check what you know about methods.
Recap
You gave your struct methods that read and even mutate its fields. Next you will compare Mojo structs with Python classes. 🧩
Frequently asked questions
Is the “Adding Methods to a Struct” lesson free?
Yes — the full text of “Adding Methods to a Struct” 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 “Adding Methods to a Struct”?
Give your type behavior. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Adding Methods to a Struct” 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
- Defining a struct
- Fields and the __init__ Method
- Adding Methods to a Struct
- Structs vs Python Classes