beginner
18 min

Functions & Scope

Learn how to create reusable code with functions and understand variable scope.

Function CreationParametersReturn ValuesLocal vs Global

1
Creating Functions

Functions are reusable blocks of code that perform specific tasks.

Basic Function

local function greet()
  print("Hello, World!")
end

greet()  -- Call the function

Functions are defined once and can be called multiple times.

Functions with Parameters

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

greet("Alice")
greet("Bob")

Parameters allow you to pass data into functions.

Multiple Parameters

local function add(a, b)
  local sum = a + b
  print(a .. " + " .. b .. " = " .. sum)
end

add(5, 3)
add(10, 20)

You can have as many parameters as needed, separated by commas.

2
Return Values

Functions can return values that can be used in your code.

Returning Values

local function multiply(a, b)
  return a * b
end

local result = multiply(5, 4)
print(result)  -- 20

-- Use directly in expressions
print(multiply(3, 7) + 10)

The return keyword sends a value back from the function.

Multiple Return Values

local function getPlayerInfo()
  local name = "Steve"
  local level = 10
  local health = 100
  return name, level, health
end

local playerName, playerLevel, playerHealth = getPlayerInfo()
print(playerName, playerLevel, playerHealth)

Lua functions can return multiple values!

3
Variable Scope

Scope determines where variables can be accessed in your code.

Local vs Global Scope

-- Global variable (accessible everywhere)
globalVar = "I'm global"

local function testScope()
  -- Local variable (only inside function)
  local localVar = "I'm local"
  print(localVar)  -- Works
  print(globalVar)  -- Works
end

testScope()
print(globalVar)  -- Works
-- print(localVar)  -- ERROR! Not accessible here

Local variables only exist in their scope. Global variables persist everywhere.

💡 Tips:

  • • Always use local variables unless you specifically need global access
  • • Local variables are faster and prevent naming conflicts
  • • Each function creates its own scope

Practice Exercises

Exercise 1: Calculate Damage

Create a function that calculates damage with attack and defense values.

Starter Code:

-- Create your calculateDamage function here


-- Test it
print(calculateDamage(50, 10))  -- Should print 40
Show Solution
local function calculateDamage(attack, defense)
  local damage = attack - defense
  return damage
end

print(calculateDamage(50, 10))

Exercise 2: Player Stats

Create a function that returns player name and level.

Starter Code:

-- Create getPlayerStats function


local name, level = getPlayerStats()
print("Player: " .. name .. ", Level: " .. level)
Show Solution
local function getPlayerStats()
  local name = "Hero"
  local level = 25
  return name, level
end

local name, level = getPlayerStats()
print("Player: " .. name .. ", Level: " .. level)

Ready for more?

Continue your learning journey

Executors.Online - Your Roblox Developer Hub