A function is kind of like a command. You input values and it returns a value.
local myFunc = function(x, y)
return x + y
end
print(myFunc(10, 20))
It will print:
30
If you do not understand feel free to ask me in the comments.
That was how a function works on a basic level HOWEVER functions ARE variables, just a different type of variable.
Variables have types:
Integer, String (text), Boolean (true or false), Table, Function, and Userdata (Which is kinda multiple types but whatever)
Functions are like a variable meaning there can be multiple functions.
The way a function is declared does not set it apart from other variables
function myFunc(x, y)
return x + y
end
print(myFunc(10, 20))
It will still print:
30
You can declare as many functions or call them as much as you would like
local function myFunc(x, y)
return x + y
end
local function anotherFunc(x, y, z)
return x + y - z
end
print(myFunc(10, 20))
print(myFunc(20, 5))
local value = anotherFunc(10, 20, 15)
print(value)
This will print:
30
25
15
There are also built in functions like newproxy (Which I'm not going to use as an example because it's not simple to use)
Say print. print IS a function along with Instance.new, Vector3.new, etc..
Doing:
print("Hello world!")
local x = Instance.new("Part")
Calls the function print which does some internal stuff to print "Hello world!" and calls the functions Instance.new which creates a new part in this case and then returns it.
Global functions using _G and shared should not be used. I would recommend modules (module scripts) instead.
1 comment
Thanks for the simple answer xD