Calling a Function
Part of the Fundamentals section of Coddy's Lua journey — lesson 51 of 90.
Creating a function is only the first step - to make the code inside the function run, you need to call the function.
Here's the key concept: declaring a function doesn't execute its code. Think of it like writing down a recipe - the recipe exists, but no food gets cooked until you actually follow the instructions.
To call a function, simply write its name followed by parentheses:
function sayHello()
print("Hello there!")
end
sayHello() -- This calls the functionChallenge
EasyCreate a player status notification system by declaring and calling a function that displays important game information. First, declare a function named displayPlayerStatus that prints "Player is online and ready!". After declaring the function, call it twice to simulate checking the player status at two different moments in the game.
Cheat sheet
To call a function, write its name followed by parentheses:
function sayHello()
print("Hello there!")
end
sayHello() -- This calls the functionDeclaring a function doesn't execute its code - you must call it to run the code inside.
Try it yourself
-- TODO: Write your code here
-- Declare the displayPlayerStatus function
-- Then call it twiceThis 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