Functions in Tables
In the lesson on tables as dictionaries, you described one thing with one table, a record such as {name = "Carrot", days = 3}. In the lesson on functions, you learned that a function is a value, like a number or a text: you can store it in a variable and pass it to another function. Put the two together, and a table can hold functions too, right next to its data:
local farm = {name = "Sunflower Farm", coins = 120}
farm.describe = function(f)
print(f.name .. ": " .. f.coins .. " coins")
end
farm.describe(farm)This prints:
Sunflower Farm: 120 coinsfarm.describe = function(f) … end stores a function under the key describe, just as farm.coins = 120 stores a number under the key coins. In farm.describe(farm), farm.describe takes the function out of the table, and the parentheses call it. The function gets the farm as its parameter f and reads the farm's values.
Lua has a shorter way to write the same thing, and you'll see it much more often: function farm.describe(f) means exactly farm.describe = function(f). There's no local in front, because the function doesn't become a new variable; it goes into the table farm, which already exists. The next example uses the short form, and it also shows that a function in a table can change the table it gets:
local farm = {name = "Sunflower Farm", coins = 120}
function farm.describe(f)
print(f.name .. ": " .. f.coins .. " coins")
end
function farm.earn(f, amount)
f.coins = f.coins + amount
end
farm.earn(farm, 30)
farm.describe(farm)This prints:
Sunflower Farm: 150 coinsWhy give the function the farm as a parameter, when it could simply use farm? Because then it works for any table with the same keys: farm.describe({name = "Grandpa Joe's farm", coins = 80}) prints Grandpa Joe's farm: 80 coins. That will matter soon, when there are many tables of the same kind.
You've been using tables of functions since the start of the course. string and math are ordinary tables, and string.upper is the function stored in the table string under the key upper:
print(type(string), type(string.upper), type(math.floor))This prints:
table function functionA table that holds data together with the functions that work with that data is called an object. This whole lesson is about objects: how to write them, how to make many of the same kind, and how to make them work with print, ==, < and +.
Typical mistake: forgetting the argument. In farm.describe(), the parameter f gets no value, so it's nil:
local farm = {name = "Sunflower Farm", coins = 120}
function farm.describe(f)
print(f.name .. ": " .. f.coins .. " coins")
end
farm.describe()The program ends with an error:
main.lua:4: attempt to index a nil value (local 'f')Indexing means reading a key with . or [ ]. f.name tries to read the key name from f, and there's nothing to read from nil.
Instructions
Emma's barn holds eggs and milk. Add a function barn.report(b) that prints what's in the barn b, and call it with barn.report(barn). The program prints:
Barn: 12 eggs, 4 liters of milkAdd a function barn.addEggs(b, count) that adds count eggs to the barn b.
The hens have laid 6 more eggs. At the end of the program, add them with barn.addEggs(barn, 6) and print the report again:
Barn: 12 eggs, 4 liters of milk
Barn: 18 eggs, 4 liters of milkStart programming for free
1/9