Help With Parameters

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

2 Likes

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)
5 Likes

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
6 Likes

Thanks I Understand it Now! Thank you sir

Thank you too sir. you both helped me understand it !

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.

Alrighty, glad I could help :+1:

wait they’re both different ? arguments and parameters are one thing right ?

Parameters are the variables. Arguments are values.

1 Like

You mean numbers like (1,2) .30charrr

function Print(v1, v2) -- Parameters
print(v1, v2)
end

Print("Hello", "World") -- Arguments
1 Like

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.

1 Like

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")
2 Likes

Oh My Bad. I’m bad at learning new things . Sorry Dude But I understood it now. Thanks you Force! . - may the force be with you

1 Like

No problem, just glad I could help and yes haha I get that one a lot, may the force be with you too.

2 Likes

Thanks for helping. You also helped a lot buddy.

Well Can you please tell me Why I’m getting a nil value with a 6 ?

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.

1 Like

Just a question. why do people use parameters and arguments ?

Well, if something similar needs to be done for many different values, parameters and arguments are useful. For example, if you had a rounding function for rounding the result of a calculation, you don’t know the result of that calculation before that calculation is made. You need to give the function the result of that calculation each time you do the calculation and call the rounding function and the result of that calculation could be different each time.

The Roblox API functions use parameters too. For example, Instance.new takes a classname as an argument, so that classname is then set as the value of some parameter. The function uses that information so that it’ll create an Instance object of the correct class everytime it’s called.

Shortly, parameters are useful when you need to do something many times with different values.

3 Likes