Arguments and Parameters
A parameter is properly known as the variable(s) in the definition of a function. An argument is properly known as the values that appear in a function getting called. Let's examine this piece of code:
In the example, x is a parameter of the function, PrintStuff. Parameters are always declared in this format, inside the parenthesis of the function declaration.
In this function, there is a single statement, print(x). This statement prints whatever value x holds to the output. Currently, even though x is not defined and is nil, the compiler will not call an error because the function has not been called yet.
Defining a function with parameters
This is the basic syntax for defining a function:
function name(argument1, arg2, arg3...) --arguments are only inserted when needed
When defining a function, you can use parameters. Parameters can be useful because functions can perform tasks for more than just one value. For example:
function AddNumbers(x, y)
return (x + y) --you might want to learn about the return statement
end
The previous code has two parameters, so when calling the function, we can insert ANY two values to the parameters x and y and return their sum, because of the return (x + y) statement. Currently, x and y have no values, but when we call the function, you will give the the parameters x and y values. Take note that because functions are stored in your computer's memory when declared, they will not call errors (unless there is a syntax error at the start) until called. If this weren't the case, it would error just like having a script like this: print(x + y) --> attempt to perform arithmetic on global 'x' (a nil value).
Parameters are very helpful for making a function that can perform multiple tasks.
Keep in mind that all parameters are local to the function's and its descending scopes.
Calling a function with arguments
Calling a function with arguments is very simple, and is similar to calling a function with no arguments. This is the basic sytanx for calling a function with arguments:
functionName(value1, value2, value3, etc) --calling any number of arguments
Keep in mind that in the above syntax, the first parameter you created in the function declaration becomes equal to value1, the second parameter equal to value2, and so on.
Let's use the previous example for this demonstration.
NOTE: When calling a function with arguments, you can use any number of arguments you want. By calling too many arguments, the code will ignore the excess arguments. By calling too little arguments, the parameters not represented will be returned as nil.
Variadic arguments
You may have realized that global functions, such as 'print()', can take an indefinite number of arguments. It will print all of its arguments to the output (with a tab space in between each printed value), no matter how many you have. For example:
print("LOL", "OMG", "C++ IS PRETTY NEAT IF YOU ASK ME", 1241425717231) --4 arguments
print(1, 122, "Hi!", "ROBLOX", "Give it your best shot!", "C++", "Interesting") --6 arguments
Check the output, and yet again, you will see all arguments printed to the output. In your custom functions, you can have an arbitrary number of arguments in them as well! The set of characters "..." represents variadic arguments. Examine the following code:
table1 = {}
function appendToTable(tab, ...)
local args = {...} --you might want to understand tables for this example
for i, v in pairs(args) do
table.insert(tab, v)
end
end
appendToTable(table1, "xD", ":D", "lol")
Functions with arguments can be very powerful, as they can be declared at the beginning of your script, and used in various ways throughout your code. Storing code that is used very often as functions can make scripting a lot easier.