Key Concepts

Review core concepts you need to learn to master this subject

lua function arguments

function printNumbers(x, y) print("Your first number is: " .. x) print("Your second number is: " .. y) end -- 3 and 25 are our arguments printNumbers(3, 25) -- Prints: -- Your first number is 3 -- Your second number is 25

When a function is called, it can contain a comma-separated list of values between parenthesis, these are arguments. They are assigned to parameters based on the order in the function declaration.

Lesson 1 of 1
  1. 1
    In programming, as we start to write bigger and more complex programs, one thing we will start to notice is we will often have to repeat the same code in many different places. Let’s imagine we ar…
  2. 2
    Let’s stick with our travel time application. distance = 60 mph = 10 time = distance / speed print(“It will take “ .. time .. “ hours to arrive.”) If we want to create a function for our code th…
  3. 3
    While declaring a function is essential, it is only half of the battle. If we want the computer to run our function’s code, the function must be called. To call a function, type the name it wa…
  4. 4
    function getTravelTime() distance = 60 mph = 10 time = distance / mph print(“It will take “ .. time .. “ hours to arrive.”) end In our getTravelTime function we have been setting the mph …
  5. 5
    As we have seen, our functions can receive outside information from the rest of the program. Our functions can also send information back. This is done through a return. Imagine we are making…
  6. 6
    One more important note about returns is that any code after a return statement will never be executed. Let’s look at a modified version of our getBoostedSpeed function. In this example, if the inp…
  7. 7
    Now that we have a strong foundation for functions, let’s practice writing a few more. For reference, here is the getTravelTime function we have been building: function getTravelTime(mph, miles) …
  8. 8
    We’ve been using print this whole time, but Lua comes with a lot more functions built in. For example, Lua has an entire collection of functions for using and manipulating strings. The **documentat…
  9. 9
    Congratulations on working through functions! Let’s review what we learned function getTravelTime(mph, miles) travelTime = miles / mph return travelTime end getTravelTime(10, 60) – returns a …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo