Concepts - Functions
What is a Function
A Function is a reusable block of code defined using the function and end keywords. Functions allow you to group statements together, promoting code organization and reusability
Example
Output
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 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
Output
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
Output
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
Output
Assignment of a variable
In Lua, you can assign a function to a variable by using the function name without parentheses.
Example
Here, the pow variable now holds the function, and you can call this function by using pow(). This technique allows you to create and store anonymous functions in variables, making it easy to pass functions around in your Lua code. Now you can pass the variable in other functions like concommand.Add.