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
endHere'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
EasyCreate 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
endExample 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 argumentThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator