Functions with Multiple Params
Part of the Fundamentals section of Coddy's Lua journey — lesson 53 of 90.
Creating a function with multiple parameters is straightforward - simply separate each parameter with a comma inside the parentheses:
function functionName(parameter1, parameter2)
-- code that uses both parameters
endHere's a practical example:
function addNumbers(a, b)
local sum = a + b
print("The sum is: " .. sum)
end
addNumbers(5, 3) -- outputs: The sum is: 8
addNumbers(10, 7) -- outputs: The sum is: 17When you call the function, you provide the values in the same order as the parameters are defined. The first value goes to the first parameter, the second value to the second parameter, and so on.
Challenge
EasyCreate a battle damage calculator that uses multiple parameters to determine total combat damage. Declare a function named calculateDamage that takes two parameters: weaponDamage and criticalBonus. Inside the function, calculate the total damage by adding the weapon damage and critical bonus together, then print the result in the format "Total damage: [result]" where [result] is the calculated sum. After declaring the function, call it with the arguments 45 and 15 to test your damage calculation system.
Cheat sheet
To create a function with multiple parameters, separate each parameter with a comma inside the parentheses:
function functionName(parameter1, parameter2)
-- code that uses both parameters
endExample with two parameters:
function addNumbers(a, b)
local sum = a + b
print("The sum is: " .. sum)
end
addNumbers(5, 3) -- outputs: The sum is: 8
addNumbers(10, 7) -- outputs: The sum is: 17When calling the function, provide values in the same order as the parameters are defined. The first value goes to the first parameter, the second value to the second parameter, and so on.
Try it yourself
-- TODO: Write your code here
-- Declare the calculateDamage function that takes weaponDamage and criticalBonus as parameters
-- Inside the function, calculate total damage and print the result
-- Then call the function with arguments 45 and 15This 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