Menu
Coddy logo textTech

Explicit 'self'

Part of the Object Oriented Programming section of Coddy's Lua journey — lesson 2 of 70.

When a function lives inside a table, it often needs to access or modify data within that same table. But here's the problem: how does the function know which table it belongs to?

The solution is to explicitly pass the table as the first argument to the function. By convention, this parameter is named self:

local counter = {
    value = 0
}

function counter.increment(self)
    self.value = self.value + 1
end

counter.increment(counter)
print(counter.value)  -- Output: 1

Notice how we call counter.increment(counter), passing the table itself as the argument. Inside the function, self now refers to that table, allowing us to read and modify self.value.

This pattern is essential because it lets the function work with the table's data without hardcoding the table's name. Consider a function that resets and returns the current count:

function counter.reset(self)
    local old = self.value
    self.value = 0
    return old
end

print(counter.reset(counter))  -- Output: 1
print(counter.value)           -- Output: 0

While passing the table manually works, it can feel repetitive. In the next lesson, you'll learn a cleaner syntax that handles this automatically.

challenge icon

Challenge

Easy

Create a table named tracker with a field count initialized to 0.

Add a function tracker.addAmount(self, amount) that:

  • Accepts self (the table) as the first parameter
  • Accepts amount as the second parameter
  • Adds amount to self.count

You will receive two inputs:

  1. First amount to add
  2. Second amount to add

Call tracker.addAmount twice (once for each input), passing the table as the first argument each time. After both calls, print the final value of tracker.count.

Cheat sheet

Functions inside tables can access table data by receiving the table as their first parameter, conventionally named self:

local counter = {
    value = 0
}

function counter.increment(self)
    self.value = self.value + 1
end

counter.increment(counter)  -- Pass the table as argument
print(counter.value)  -- Output: 1

The self parameter allows the function to read and modify the table's fields without hardcoding the table name:

function counter.reset(self)
    local old = self.value
    self.value = 0
    return old
end

print(counter.reset(counter))  -- Output: 1
print(counter.value)           -- Output: 0

Functions can accept additional parameters after self:

function counter.add(self, amount)
    self.value = self.value + amount
end

counter.add(counter, 5)

Try it yourself

-- Read input
local amount1 = tonumber(io.read())
local amount2 = tonumber(io.read())

-- TODO: Write your code below
-- Create a table named 'tracker' with a field 'count' initialized to 0
-- Add a function tracker.addAmount(self, amount) that adds amount to self.count
-- Call tracker.addAmount twice with the table as the first argument


-- Output the final count
print(tracker.count)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Object Oriented Programming