Menu
Coddy logo textTech

Functions with Parameters

Part of the Fundamentals section of Coddy's Lua journey — lesson 52 of 90.

Parameters allow you to pass information into a function when you call it. Think of them as placeholders that receive values from the outside world. Here's the basic syntax:

function functionName(parameter)
    -- code that uses the parameter
end

Here's a practical example:

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

greetUser("Alice")  -- outputs: Hello, Alice!
greetUser("Bob")    -- outputs: Hello, Bob!

In this example, name is the parameter. When you call greetUser("Alice"), the value "Alice" gets passed into the function and becomes available as the name variable inside the function.

challenge icon

Challenge

Easy

Create a personalized quest assignment system using function parameters. Declare a function named assignQuest that takes one parameter called playerName. Inside the function, use string concatenation to create and print a message that says "Quest assigned to [playerName]: Defeat the Dragon!" where [playerName] is replaced with the actual parameter value. After declaring the function, call it with the argument "Hero" to test your implementation.

Cheat sheet

Parameters allow you to pass information into a function when you call it. They act as placeholders that receive values from the outside world.

Basic syntax for functions with parameters:

function functionName(parameter)
    -- code that uses the parameter
end

Example with a parameter:

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

greetUser("Alice")  -- outputs: Hello, Alice!
greetUser("Bob")    -- outputs: Hello, Bob!

When you call a function with an argument, that value gets passed into the function and becomes available as the parameter variable inside the function.

Try it yourself

-- TODO: Write your code here
-- Declare the assignQuest function that takes playerName as parameter
-- Inside the function, create and print the quest assignment message
-- Then call the function with "Hero" as the argument
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals