Variables

Programs need to remember values: how many coins Emma has, how many eggs her hens laid this morning, what her farm is called. For that, they use variables. A variable is like a box with a label: it has a name, and it holds a value that the program can use later.

In Lua, you create a variable like this:

local coins = 120
print(coins)

This prints:

120
  • local says that you're creating a new variable.
  • coins is its name. You choose it yourself.
  • = stores the value on the right in the variable. In programming, it doesn't mean “equals” as in math. Read it as “store”: 120 is stored in coins.
  • print(coins) prints the value that's stored in coins.

To use the value, write the name of the variable wherever you'd write the value itself. It works in calculations, in joined texts and in print with commas:

-- Emma's morning
local farmer = "Emma"
local coins = 120
print("Coins: " .. coins)
print(coins + 30)
print(farmer, "has", coins, "coins")

This prints:

Coins: 120
150
Emma	has	120	coins

A variable can hold a number or a text, as farmer does. The name of the variable goes outside the quotes: print("coins") prints the word coins, while print(coins) prints the value, 120.

Why local? A local variable belongs only to the part of the program where you created it. Without local, Lua creates a global variable, which every part of a bigger program can see and change, and that leads to mistakes that are hard to find. So in this course, every variable is local. You'll learn the whole story in the lesson on functions.

Rules for the names of variables:

  • A name can contain letters, digits and the underscore _, but it can't start with a digit: plot2 is fine, 2plot isn't.
  • A name can't contain spaces or hyphens. If it has more words, write them together, and start every word after the first one with a capital letter: seedPrice, eggsToday. This style is called camelCase, because the capitals stick up like the humps of a camel. This course uses it for all names.
  • Use only the plain letters a to z: cafe, not café. Lua doesn't allow letters with accents in names: local café = 1 ends with the error unexpected symbol near '<\195>', because Lua doesn't know what to do with the é.
  • Uppercase and lowercase letters matter: coins and Coins are two different names.
  • A name can't be a keyword, a word that already means something in Lua, such as local, end, nil, true or false. local end = 3 ends with the error <name> expected near 'end': Lua expected a name, but found a keyword.

Choose names that say what the variable holds: eggs tells you more than e or x.

A name that breaks the rules is a syntax error, so nothing in the program runs:

local 2plot = "carrots"

Lua reports:

main.lua:1: malformed number near '2p'

Lua sees the digit 2 and starts reading a number. Then it finds the letter p, which doesn't belong in a number. A malformed number is a number that's written wrong.

Instructions

The hens laid 12 eggs this morning. Below the print, create a local variable eggs with the value 12.

On the next line, print the eggs using the variable, so that the program prints:

Good morning, Emma!
Eggs: 12

Emma has 4 hens. Below, create a variable hens with the value 4, and print one more line. Take all three values from the variables farmer, hens and eggs, and join them with ..:

Good morning, Emma!
Eggs: 12
Emma's 4 hens laid 12 eggs.

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with:

1/10