Menu
Coddy logo textTech

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
end

Here'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: 17

When 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 icon

Challenge

Easy

Create 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
end

Example 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: 17

When 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 15
quiz iconTest yourself

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

All lessons in Fundamentals