← Back to course
review ✦ 50 Studs

Your First Recipe

Write a function once, call it as many times as you want.

A function is a recipe. You write the steps once, give the recipe a name, and from then on you just call the name instead of rewriting the steps. Write once, use forever. That’s the whole pitch, and it’s a big one.

local function wave()
  print("Hi there!")
end

wave()
wave()

Two parts to notice. Defining the function (the local function wave() ... end block) does not run it, it just teaches Lua the recipe. Calling it (wave() with the parentheses) is what actually runs the steps. New coders sometimes write a beautiful function and wonder why nothing happens. Almost always, they forgot to call it.

Why bother? Because the moment you’d copy-paste the same few lines a second time, a function is the better move. Need a change later? You fix it in one place instead of hunting down every copy. Your future self will thank you.

Now make it yours: write a greet function that prints a hello, then call it three times. Once it works, change the message inside the function and watch all three calls update at once.

Output
Click Run to see your output.