Defining Functions

In this lesson, you'll write your own functions. You already know several functions that come with Lua: print, math.floor, table.insert. Each of them is a piece of code that someone wrote once and gave a name, so that you can run it with a single line, as many times as you need.

Your own functions help in the same way. Every morning, Emma does the same chores, and a program that lists them for a whole week would repeat the same lines seven times. If she wanted to add a chore, she'd have to add it in seven places, and she'd surely miss one. Instead, you write the lines once and give them a name. Such a named piece of code is a function. This program creates a function with Emma's chores and then runs it for two days:

local function morningChores()
    print("Feed the hens")
    print("Milk Bella")
end

print("Monday")
morningChores()
print("Tuesday")
morningChores()

This prints:

Monday
Feed the hens
Milk Bella
Tuesday
Feed the hens
Milk Bella

The first four lines create the function. That's called defining it, and the definition has these parts:

  • local function starts the definition. local means the same as with variables: the function belongs to this program. Later in this lesson, you'll see why that matters.
  • The name, here morningChores. The same rules apply as for variable names, and it's written in camelCase too. Choose a name that says what the function does.
  • Parentheses (). They're empty for now; in the next exercise, you'll find out what goes in them.
  • The body: the lines the function runs, indented by 4 spaces.
  • end, which closes the function, as it closes if and loops.

A definition alone doesn't run anything. Without the lines below it, the program would print nothing, and that wouldn't be a mistake: a definition only tells Lua what the function should do, like a recipe written in a notebook. To run the function, you call it: you write its name and parentheses, morningChores().

Lua runs the program from top to bottom. It goes past the definition and only remembers the function. Then it prints Monday, and at the call, it jumps into the function, runs its body and continues with the line after the call. The calls aren't indented, because they aren't part of the body. The empty line after end isn't required, but it makes the program easier to read.

The parentheses are what make it a call. The name alone, morningChores on a line of its own, is a syntax error. And print(morningChores) doesn't run the function either: it prints something like function: 0x10ead0, which is how Lua prints the function itself.

A function exists only from the line where it's defined. Calling it earlier is a common mistake:

morningChores()

local function morningChores()
    print("Feed the hens")
end

The program ends with an error:

main.lua:1: attempt to call a nil value (global 'morningChores')

On line 1, Lua doesn't know any morningChores yet, so the name has the value nil, and nil can't be called. (What global means, you'll learn later in this lesson.) You get the same error when the name in the call is spelled differently from the definition, for example MorningChores(): upper and lower case letters matter. That's why functions are defined at the top of the program and called below.

Instructions

On every market day, the program prints the day and the sign of Emma's stall, so the three lines of the sign are written twice. Below the comment at the top, define a local function printSign whose body prints the three lines of the sign. Run the program: it prints the same as before, because nobody calls the function yet.

Replace the three lines of the sign below Monday and below Tuesday with a call of printSign. Then add a third market day: print Wednesday and call printSign once more. The output ends with:

Wednesday
==============
Sunflower Farm
==============

Emma wants the sign to say what she sells. In the body of printSign, add a line that prints Fresh vegetables, between Sunflower Farm and the bottom line. Run the program: all three signs have changed. You changed one place in the code, and all three calls do something new. That's what functions are for. The program prints:

Monday
==============
Sunflower Farm
Fresh vegetables
==============
Tuesday
==============
Sunflower Farm
Fresh vegetables
==============
Wednesday
==============
Sunflower Farm
Fresh vegetables
==============

Start programming for free

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

Or sign up with:

1/11