The 'return' Statement
Part of the Fundamentals section of Coddy's Lua journey — lesson 54 of 90.
The return statement allows a function to send a value back to the code that called it. This is fundamentally different from printing - instead of displaying something on screen, the function gives you a value that you can store in a variable or use in calculations.
Here's the basic syntax:
function functionName(parameter)
local result = parameter * 2
return result
endWhen you call this function, it doesn't just perform an action - it gives you back a value:
local doubled = functionName(5)
print(doubled) -- outputs: 10The key difference is that returned values can be stored, passed to other functions, or used in expressions.
Challenge
EasyCreate a power calculation system that returns computed values instead of just printing them. Declare a function named calculatePower that takes one parameter called baseNumber. Inside the function, calculate the square of the base number (multiply it by itself) and use the return statement to send this result back. After declaring the function, call it with the argument 7, store the returned value in a variable called result, and print the result.
Cheat sheet
The return statement allows a function to send a value back to the code that called it, instead of just printing to the screen.
Basic syntax:
function functionName(parameter)
local result = parameter * 2
return result
endUsing the returned value:
local doubled = functionName(5)
print(doubled) -- outputs: 10Returned values can be stored in variables, passed to other functions, or used in expressions.
Try it yourself
-- TODO: Write your code here
-- Declare the calculatePower function that takes baseNumber as parameter
-- Calculate the square of baseNumber and return it
-- Call the function with argument 7 and store result in a variable
-- Print the resultThis 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