|
Please tell me if something in here is inaccurate. I highly doubt anything is, but I would like to know if there is anything wrong.
1) A function is a data type. The wiki (idiotically) doesn't mention this fact, but I am a writer and will fix that when the wiki is fixed. Let me prove it to you:
print(type(function() end)) -- Output: function (type returns the data type)
2) Because functions are a data type, you can treat them like you treat normal variables.
Having said those two things, I will now attempt to explain how functions work.
Declaration:
There are a few ways you can declare a function. You can declare them like:
function funcName()
which is the EXACT SAME AS
funcName = function()
OR
local function funcName()
which is the EXACT SAME AS
local funcName; funcName = function()
AND NOT THE EXACT SAME AS
local funcName = function()
Great. But now what do we do? We can create whatever we want inside of this. Please note that "function" is a keyword that creates a new scope, which, in builders' terms, means that you need another end.
function functName()
print("Hello")
end
WHAAAT??? NOTHING HAPPENED! That's what's so amazing about functions: you can call them whenever you want.
Calling:
functName()
Done. If we had the code above, too, that would print Hello to the output. We can call a function as many times as we want.
functName()
functName()
-- Hello
-- Hello
Yay! But then what are things like functionName(Part) supposed to mean? Why, functionName(Part) is the call, and Part is the argument! Well, what are arguments? They are things you pass to parameters? Well, what are parameters D=? Simple:
Arguments and Parameters:
Add = function(Num1, Num2) -- Num1 and Num2 are parameters!
-- Same as function Add(Num1, Num2)
print(Num1 + Num2)
end
Add(3, 4) -- 3 and 4 are arguments!
-- 7
Let's examine what happened here. Add was a function DECLARED with two PARAMETERS, Num1 and Num2. We CALLED FUNCTION Add with two ARGUMENTS, 3 and 4. The FUNCTION added the two numbers, and printed the result to output. As with all FUNCTIONS, we can do this as many times as we want.
Add(3, 4)
Add(5, 6)
Add(1, 7)
Add(4, 9)
-- 7
-- 11
-- 8
-- 13
Pretty useful, huh? Well, how do we declare parameters and values? Like this:
funcName(Parameter1, Param2, Param3, Param4, Param5, Param6, Param100000) for as many parameters as we want. Arguments are even easier:
funcName(Value1, Value2, Value3, Value4, Value5, Value6, Value100000)
Let me give you another example:
function Divide(Dividend, Divisor)
quotient = Dividend/Divisor
print(quotient)
end
Divide(4, 2)
Divide(4, 3)
Divide(4, 4)
Divide(4, 5)
-- 2
-- 4/3 (1.3 repeating 3)
-- 1
-- 4/5 (0.8)
Now, onto the second-to-last thing I'm going to talk about in this post. The "return" keyword. This is where functions truly come in handy. What if I wanted to give back that sum? Or that quotient? Easy! Using return, of course.
function Divide(Dividend, Divisor)
quotient = Dividend/Divisor
return quotient -- THERE!
end
function Add(Num1, Num2)
return Num1 + Num2 -- Here :D
end
print(Divide(Add(3, 4), Add(5, 2)))
-- 1
What this script does is return the quotient of the sum of 3 and 4 and the sum of 5 and 2, which it prints. Basically, it adds 3 and 4 (7) and divides that by the sum of 5 and 2 (7), resulting in 1.
The last thing I'm going to cover in this post is events. Events are things that can cause a function to be called, such as the "Touched", the "Chatted", and the "Clicked" events. You use these rather simply:
Event:connect(funcName)
Part.Touched:connect(Kill)
Done.
Also, do not call something an "OnTouched" script or an "OnChatted" script or an "OnClicked" script. Call them by the event. A "Touched" script (technically, function), a "Chatted" script, and a "Clicked" script. OnTouched is simply the name whoever wrote the script gave to the function that handled the Touched event. Part.Touched:connect(OnTouched) -- OnTouched is the function that handled the Touched event.
Please, feel free to ask me questions in this thread about functions. |