Hello! I have been taking a recent look at functions in general to refresh my memory, and I have understood the first 2 ways that functions can be produced.
First off, you have this way:
myFunction = function()
print("Hello World!")
end
I believe that this basically creates a variable that has the value of a function that does certain code, so you can call it by typing that variable name.
Secondly, you can create a function with this shorter and easier way:
function myFunction()
print("Hello World")
end
This method is used much more, and for good reason, it’s a lot easier to read and it makes it very very very simple.
However, I am extremely confused about anonymous functions, here’s 2 examples…
- Using an event.
part = script.Parent
part.Touched:Connect(function()
print("Hello World!")
end)
The way it is used here just confused me, the way it’s formatted such as the function keyword itself being surrounded by () and we have to add another pair of parenthesis around the entire code and I don’t understand how this works at all and would love to see it broken down.
- In a table
myTable = {
function()
print("Hello World!")
end,
}
In this example, the name of the function doesn’t even have to be mentioned yet it still would function normally.
How do these anonymous functions actually work? What makes it so that they don’t error, when do we use them. I would just like to see a thorough breakdown of both examples and why we have to format it in these ways. Thanks in advance.