of     1   

Merlin11188
#59199063Wednesday, December 07, 2011 12:42 AM GMT

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.
SCARFACIAL
#59200559Wednesday, December 07, 2011 1:02 AM GMT

Pretty good guide, but could definitely be improved and cleaned up a bit. Couple things I would add: Variable number of arguments: function add(...) -- We don't have to define each argument local sum = 0; for i,v in ipairs(arg) do -- arg returns a table of each argument sum = sum + v; print(i,v); end return sum; end print(add(1,2,3)) --> 6 print(add(13,37)) --> 50 Although you should keep in mind that the 'arg' table has one more value in it than arguments, who's index is 'n' and value is the number of args. In most cases you can accomplish whatever you want to do by simply using ipairs, which will ignore this last value since it's index is not numerical. Anonymous functions: Just like any other function, but you don't have to define it to a variable because you call it directly. Event:connect(function() --[[ Function stuff here ]] end) script.Parent.Touched:connect(function(hit) hit:remove(); end) "I WILL GIVE YOU BACON." - Deadmau5 ~Scarfacial
Merlin11188
#59201729Wednesday, December 07, 2011 1:16 AM GMT

Thanks. I left those out because I didn't want to bombard them with too much information. I thought about doing it, though :D
Merlin11188
#59216174Wednesday, December 07, 2011 6:57 AM GMT

Bump.

    of     1