Arguments and Parameters - roblox lua

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:

Light Theme local function printStuff(x) --this is a function, and it has one parameter, x.

printStuff("Hello world.") --"Hello world." is the argument

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,


. This statement prints whatever value x holds to the output. Even though x is not defined and is nil, the compiler will not throw 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:

Light Theme local function addNumbers(x, y)
return (x + y) --you might want to learn about the return statement

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, ...)

--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.
Light Theme local function addNumbers(x, y)
return (x + y)

local sum = addNumbers(2, 5) --notice how I call the function (keep in mind that when returning a value, the function holds the returned value
print(sum) -- 7


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 


, 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:

Light Theme 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") --7 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:
  1. Light Theme local table1 = {} local function appendToTable(tab, ...)local args = {...} --you might want to understand tables for this examplefor i, v in pairs(args) dotable.insert(tab, v)endend appendToTable(table1, "xD", "Big Grin", "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.

Users browsing this thread:
1 Guest(s)