Return Values: What You Will Learn
In the last lesson, you passed information into functions using parameters. In this lesson, you will learn how functions can send information back using return values. Instead of only printing inside a function, you will compute a result and return it so the caller can store it, print it, or use it later.
Define a Function That Returns a Value
Explanation:
- The first two lines create data we will use: a table of
country_costsand a list of selected countries. - The function
calculate_trip_cost(countries, costs)takes two parameters: a list of countries and a lookup table of costs. - It initializes
total_costto 0, loops through the chosen countries withipairs, and adds each country’s cost usingcosts[country]. return total_costsends the computed total back to whoever called the function. The function itself does not print.
Capture and Use the Returned Value
Explanation:
- The function call produces a value. We store that value in
trip_cost. - We then print a readable message by concatenating the string with
trip_costusing... - Returning a value lets you reuse it anywhere in your program, not just print it once.
Returning Multiple Values
In Lua, a function can return more than one value by separating them with commas in the return statement.
Explanation:
- The function
min_and_max(a, b)compares two numbers and returns both the smaller and the larger value. - When calling the function, you can assign each returned value to its own variable.
- This is useful when you want a function to provide more than one result at a time.
Summary and Next Steps
You learned how to:
- Compute a result inside a function and return it with
return. - Pass data into a function via parameters and get data back as a return value.
- Store that returned value in a variable and print it when needed.
Great work advancing from parameters to return values. Head to the practice section to apply these ideas and see how returning values makes your functions more powerful and reusable.

Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal