I’ve been learning Lua for the past couple of days and I’ve been stuck on Parameters. Like What is a parameter, why do people use it, whats the use of it. Also It’ll be nice if you guys could tell me that in Simple Words Cause I’m not any professional at coding right now.Thank you
For a function to give you results, you need to give it some initial variables first, when you add 2 to 4, you get 6, but without you telling the add function what numbers to add, it would have never knew what numbers to add, same with functions, you tell them what do you want and then they give you a result
Think of parameters as the placeholders for the values you put in the parenthesis when you call a function. The values you pass in between the parenthesis are called arguments.
Example 1:
function Print(v1, v2)
print(v1) -- v1 will be replaced with "Hello"
print(v2) -- v2 will be replaced with "World"
end
Print("Hello", "World")
Example 2:
function Add(v1, v2)
print(v1 + v2) -- v1 will be replaced with 1 and v2 will be replaced with 2
end
Add(1, 2)
Parameters are variables that are written between the () when creating a function. When calling a function, it’s possible to give values to those parameters. Not every function needs parameters, but they are often necessary.
Here’s an example.
local function myFunct(parameter1, parameter2) -- these two are parameters, they can have any name a variable can have.
-- this example function prints true or false depending on if the parameters have the same value.
print(parameter1 == parameter2)
end
-- You can put values into the parenthesis when calling a function. These values are called arguments.
-- The values of the parameters are set to these values when the function is called. If you don't give a parameter a value, its value will be nil when the function runs.
myFunct(3, "k") -- 3 will be the value of parameter1 and "k" will be the value of parameter2
I think you have arguments and parameters mixed up, parameters are the placeholders of a function that are replaced with the arguments you pass. I used to mix them up all the time so no worries.
Yes. The parameters are variables. Variables are references to values. So the parameters are references to the arguments. When you call a function, you can put a variable in the parenthesis (or any other reference to a value). But as variables refer to their values, you don’t actually give the function that variable. You give the function the value that the variable refers to and that value will be the value that the parameter of the function will refer to.
Its important to note that if you don’t pass an argument for a parameter it will be nil. So the order in which you write in the arguments is important.
function Print(v1, v2, v3)
print(v3) -- It will print nil
end
Print("Hello", "World")
You are only giving the function one value, which is the sum of 2 and 4, which is 6. So six is set as the value of A. You aren’t giving a value to the parameter B. Because B wasn’t given a value, it’s nil. If you want to give it "2" and "4", don’t add them together when calling the function, instead, separate them with a comma like this: Test("2", "4") And if you want to add them together in the function and print the sum, replace print(A, B) with print(A+B). Also, if you just want to calculate the sum of 2 and 4, they don’t need to be strings, they can be numbers.