Garry's Mod Wiki

Revision Difference

function#551885

<cat>Dev.Lua</cat> Functions in lua can also be put into variables just like a number or a string can. ⤶ # What is a Function ⤶ A Function is a reusable block of code defined using the function keyword and end keyword. Functions allow you to group statements together, promoting code organization and reusability⤶ ## Example⤶ ⤶ ```⤶ local function pow()⤶ print("The Result of 2 * 2 is " .. 2 * 2)⤶ end⤶ ⤶ pow()⤶ ```⤶ ⤶ ##Output⤶ ⤶ ```⤶ The Result of 2 * 2 is 4⤶ ``` local function killAllPlayers()⤶ for _, ply in ipairs(player.GetAll()) do⤶ ply:Kill()⤶ end⤶ ⤶ # Parameter⤶ ⤶ Functions with parameters in Lua allow you to pass values into the function when it's called, enabling the function to perform specific actions based on those input values. Parameters act as placeholders for the actual values that will be used in the function. By using parameters, functions can be more versatile and reusable, accepting different inputs and producing different outputs based on the provided arguments.⤶ ## Example⤶ ⤶ ```⤶ local function pow(num)⤶ print("The Result of " .. num .. " * " .. num " is " .. num * num)⤶ end killAllPlayers()⤶ ⤶ pow(2) ⤶ ```⤶ ⤶ ## Output⤶ ⤶ ```⤶ The Result of 2 * 2 is 4⤶ ``` They can be fed to other functions and methods, such as <page>concommand.Add</page>, also be stored in tables.⤶ # Return⤶ ⤶ The return statement is used in a function to specify the value(s) that the function should produce or output. When a function encounters the return statement, it immediately stops executing and returns the specified value(s) back to the caller. This allows functions to provide results to the code that called them, enabling the use of the function's output in other parts of the program.⤶ ⤶ ## Example⤶ ⤶ ```⤶ local function pow(num) ⤶ return num * num⤶ print("I will never be printed")⤶ end⤶ ⤶ local number = pow(2)⤶ ⤶ print("The Result of 2 * 2 is " .. number)⤶ ```⤶ ⤶ ## Output⤶ ⤶ ```⤶ The Result of 2 * 2 is 4⤶ ```⤶ ⤶ The return statement can be used without a value to simply exit the function, terminating its execution without returning any specific value. This can be useful when you want to stop the function at a certain point without providing an output back to the caller.⤶ ⤶ ## Example⤶ ⤶ ```⤶ local function giveMoney(ply, money)⤶ if !ply:IsAlive() then return⤶ -- Some code to add money⤶ ⤶ print("Money has been added")⤶ end⤶ ⤶ giveMoney(Entity(1), 100)⤶ Entity(1):Kill()⤶ giveMoney(Entity(1), 100)⤶ ```⤶ ⤶ ## Output⤶ ⤶ ```⤶ Money has been added⤶ ⤶ -- No Output because the Player is Dead⤶ ```⤶ ⤶ #Assignment of a variable⤶ ⤶ In Lua, you can assign a function to a variable by using the function name without parentheses.⤶ ##Example⤶ ⤶ ```⤶ local pow = function(num)⤶ return num * num⤶ end⤶ ⤶ ```⤶ ⤶ Here, the pow variable now holds the function, and you can call this function by using pow(). This technique allows you to create anonymous functions and store them in variables, making it easy to pass functions around in your Lua code.⤶ Now you can pass the variable in other function like <page>concommand.Add</page>.⤶ ```⤶ local pow = function(ply, cmd, args)⤶ local num args[0] ⤶ return num * num⤶ end⤶ ⤶ concommand.Add("pow" , pow)⤶ ``` concommand.Add("kill_all_players", killAllPlayers)⤶ ```