Lesson 8 of 15

Functions

Functions

Functions in Lua are defined with the function keyword:

function greet(name)
  print("Hello, " .. name .. "!")
end

greet("Lua")  -- Hello, Lua!

Functions can return values:

function add(a, b)
  return a + b
end

print(add(3, 4))  -- 7

Local Functions

Use local to restrict a function's scope:

local function square(x)
  return x * x
end

Your Task

Write a function multiply that takes two numbers and returns their product.

JS Transpiler loading...
Loading...
Click "Run" to execute your code.