Hi there devs.
I wanted to know one thing, how do arguments in functions actually work, i think i just dont understand.
Can someone explain please?
for example
local function uhh(argumentWhichIsSomethingToPrint)
print(argumentWhichIsSomethingToPrint)
end
uhh("this is argument one")
Or you can send tables or parts or anything.
Some examples:
local function calculate(a, b) -- two arguments
return a + b -- returning arguments
end
print(calculate(1, 10)) -- this func prints 11
-- Another
local function getUserId(player) -- one arguments
return player.UserId
end
game.Players.PlayerAdded:Connect(function(plr)
print(getUserId(plr)) -- print UserId of player
end)
i kinda didn t understand, can you explain a bit better please?
if you were to make a function to clone a part, then you have a argument in that function named “Part” and you clone the argument and parent it.
When you call it, you do
ClonePartFunction(workspace.Part)
You can think of functions as a group of statements which perform a certain task with (optional) inputs, commonly known as parameters
or arguments
to a function.
When defining a function, you’re given the ability to define arguments (inputs) which will be later used in the function
For example:
--// no inputs/arguments defined
function myFunction()
end
--// one argument named "argument1" defined
function myNewFunction(argument1)
end
Now lets make another simple function which prints something to the console
function outputMessage(message)
print(message)
end
outputMessage("Hello, World!") --// prints "Hello, World!" to the console
outputMessage("I am learning about function arguments!")
Let’s now create a function which calculates the circumference of a circle.
function circumference(radius)
return 2 * radius * 3.14
end
circumference(3) --// radius is now equal to 3, will return 2 * 3 * 3.14 = 18.84
circumference(9) --// radius is now equal to 9, will return 2 * 9 * 3.14 = 56.52
you may think like its data tranfer.
local Calculator(a,b) --a and b are the values that requires to run the function
return a + b --returns the result
end
print(Calculator(5,5)) --will print 10 because the function returned the result as 10 and we printed it.