Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

What Are Function Parameters??

Asked by
llrvs 15
3 years ago

By the title's name you probably could tell that I am a beginner scripter. I would love if someone could tell me what are function parameters in "baby words". Are they just like variables? Can someone please tell me the use for it?

4 answers

Log in to vote
3
Answered by
Axenori 124
3 years ago
Edited 3 years ago

As MineBlox_Artz mentioned, they are basically variables to a function which are created upon making a function and assigned to upon it being called. I'd like to add some things though.

First off, the parameters' scope is local to the function, ie. you can NOT use them outside it unlike if you were to make a variable above the function (assuming it is not in something else).

local function DoSomething(number)
print(number^2)
end

print(number) --THIS WOULD ERROR

Next off, if you add extra arguments (arguments and parameters are closely related, the difference between them is that parameters are defined upon function creation and arguments are used when calling it, basically arguments are the values for the parameters which will be used in the function.) to a function upon calling, they will be disregarded

local function sum(number,numberTwo)
local sum = number + numberTwo --This variables scope is also inside the function and can not be used outside it
end

sum(1,2,56) --the third parameter, 56, is disregarded.

If you add less than specified they are set to nil (nothing, not zero, nothing)

Functions are also able to be anonymous functions. Note though they do not have a name and therefore can not be called, their parameters/arguments must be defined upon creating them. Kind of weird to explain so I'll just give an example.


--Also, almost forgot to say but when using them in certain events like PlayerAdded their values will always be assigned the same way (values are always assigned in an order, though their value does not need to be the same. For example it will always assign the player to the first parameter and nothing else but the player is not always the same) and as such you should use the according number of parameters. You can find more about them on the API reference manual local Players = game:GetService("Players") Players.PlayerAdded:Connect(function(player) --the 'player' parameter is assigned the value of the player which just connected. The function is then executed and prints their name. print(player.Name .. " joined the game!") end) --Note that the ending ) must be placed after 'end'

Also, Luau added a new type of function, a variadic function. It takes in a varying number of args. it's best to explain with lots of examples, and as I don't want to make a mess of code out of this explanation I'll just link a great roblox article about them down at the bottom of the post.

Now, this is basically everything connected to parameters, I hope I didn't miss something! If you'd like to learn more about functions and how to use them (for example using 'return' to use variables scoped in the function outside it. Anyways, heres some great articles to get you started on functions:

https://developer.roblox.com/en-us/articles/Method

--Methods are a type of function, look at this later as it requires some very basic OOP. https://developer.roblox.com/en-us/articles/Method

--These are a new feature to Luau as I mentioned https://developer.roblox.com/en-us/articles/Variadic-Functions

0
i like to use parameters to troll script kiddies that use dex and view scripts lol so like game.Players.PlayerAdded:Connect(function(goawayhacker) (insert code here) end) NGC4637 602 — 3y
0
Variadic functions already exist in Lua, Roblox did not add them. Please refer to https://www.lua.org/pil/5.2.html DeceptiveCaster 3761 — 3y
Ad
Log in to vote
1
Answered by 3 years ago

They are variables specific to a function. Their names are assigned when you create a function and you can assign their values when you call the function for example

function DoThis(player)
  print(player.Name)
end
DoThis(game.Players:GetChildren()[1])
Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Basically, a variable without "=", yes.

an example would be

local function thing(message)
    print(message)
end

thing("example")

EDIT: you may also notice that there are built-in parameters for certain inbuilt events like touched & PlayerAdded so that's another example of what you can use parameters for..

you can also put multiple values too, an example would be

local function newThing(message,message2)
    print(message..message2)
end

newThing("example","example2")
Log in to vote
0
Answered by
iactz 15
3 years ago
Edited 3 years ago

They are really useful while using functions, they are variables you can change while using a function for example below.

function friend(name)
    print("You now have a new friend named "..name) --.. adds another string/variable to the end of another string/variable
end
friend("iactz")

Which would print in the console, "You now have a new friend named iactz".

Functions can also have multiple variables in some like for example

function friend(name, method)
    if method == "send" then
        print("You sent "..name.." a friend request!")
    end
    if method == "accept" then
        print("You accepted"..name.."s friend request!")
    end
end
friend("iactz","send")

Would print in the console, "You sent iactz a friend request!"

Hope this helped [even though several people already answered lol]

Answer this question