0Pricing
Zig Academy · Lesson

Methods and the self Parameter

Attach behavior to your structs.

Methods and the self Parameter is a free Zig 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 Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Data Plus Behavior

A struct can hold more than fields. You can declare methods inside it: functions that act on the data they belong to.

Functions Live Inside

Write a normal fn inside the struct body. It becomes namespaced under the type, callable as Type.name.

const Point = struct {
    x: i32,
    y: i32,
    fn origin() Point {
        return Point{ .x = 0, .y = 0 };
    }
};

The self Parameter

A method that works on an instance takes the value as its first parameter, named self by convention.

fn magnitude(self: Point) i32 {
    return self.x + self.y;
}

Call With a Dot

Call a method through an instance with a dot. Zig passes the instance as self automatically for you.

const p = Point{ .x = 3, .y = 4 };
const m = p.magnitude();

self Is Just an Argument

There is no hidden magic. The dot call p.magnitude() is exactly the same as Point.magnitude(p).

const m = Point.magnitude(p);

Reading self by Value

Taking self by value gives you a copy. That is fine for reading fields, but changes to it will not stick.

fn sum(self: Point) i32 {
    return self.x + self.y;
}

Mutating Needs a Pointer

To change fields, take self as a pointer with *Point. Now writes go to the original instance, not a copy.

fn moveRight(self: *Point) void {
    self.x += 1;
}

Auto-Reference on Call

When a method wants *Point, Zig takes the address of your var for you. You still just write p.moveRight().

var p = Point{ .x = 0, .y = 0 };
p.moveRight();

Use @This for the Type

Inside the body you can refer to the enclosing type with @This(), handy when the type has no outer name yet.

const Counter = struct {
    n: i32,
    fn get(self: @This()) i32 {
        return self.n;
    }
};

Methods Without self

A method need not take self at all. Constructors and helpers often skip it and act as plain namespaced functions.

fn init(x: i32, y: i32) Point {
    return Point{ .x = x, .y = y };
}

Organizing With Methods

Grouping behavior next to data keeps your code tidy. Methods make a struct read like a small, self-contained component.

Quick Check

What does a method need to change an instance's fields?

Recap

You added methods: functions inside a struct, with self as the instance. Use self: Point to read and self: *Point to mutate. 🔧

Frequently asked questions

Is the “Methods and the self Parameter” lesson free?

Yes — the full text of “Methods and the self Parameter” is free to read here on the web, and the Zig 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 Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Methods and the self Parameter”?

Attach behavior to your structs. You practise Zig 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 Zig Academy?

No prior experience is required. Zig 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 “Methods and the self Parameter” 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 Zig Academy lesson?

Yes. Every Zig 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. Declaring Structs and Fields
  2. Methods and the self Parameter
  3. Default Field Values
  4. Anonymous Structs and Tuples
← Back to Zig Academy