How do Anonymous Functions work, and why do they work?

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…

  1. 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.

  1. 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.

2 Likes

Anonymous Functions work by inserting a template/pseudo name.

Refering to events, the function is passed as a parameter and called by another script.

function CustomFunction(callback: () -> () )
	callback()
end

CustomFunction(function() print("Hello World") end) -- prints Hello World

Refering to tables, the function can be called as myTable[1] () (space due to devforum formatting)

local myTable = {
	function()
		print("Hello World!")
	end,
}

myTable[1]()

If the template/pseudo name is the same, then the function errors

By understanding it a bit more; Im pretty sure you know how functions in module scripts work. Like this

function module.Test()

end

This is essentially as writing as:

module.Test = function()

end

or

local module = {
	["Test"] = function()
		
	end,
}

We are assigning a template/pseudo name for the function in order to call it for our script

3 Likes

Another issue, I don’t understand the formatting of the event anonymous function.

In my example:

part = script.Parent

part.Touched:Connect(function()
	print("Hello World!")
end)

Why is it that the “function” keyword is surrounded by ()? It starts with the “(” then has the function keyword, the () as normal but then it has a “)” after the end. Why is it like that?

Your explanation of an anoymous function for the events doesn’t make as much sense because you gave a name to the function for the first one. im pretty sure that anonymous functions dont have names

Also, what is a callback?

I am NOT trying to be rude by any means but I hope that you can maybe more thoroughly read what i said about the examples i provided and my questions

2 Likes

Part.Touched:Connect() is just a function. So the reason the function is surrounded by parenthesis is because you’re passing it in as a parameter to another function.

When you give names to functions, you’re just defining a variable with a link to the function. You’re not actually naming the function.

3 Likes

it has the () as parameters can be passed in, in your example it’d the TouchingPart that would be parsed in.

-- Case 1
function Test(TouchingPart) -- function Name called Test
print(TouchingPart.Name)
end

part.Touched:Connect(Test) -- <- real function name

-- case 2

part.Touched:Connect(function(TouchedPart) -- <- same function as Test just without the Name
print(TouchedPart.Name)
end)

Further adding onto this, as you can see in the first example I give a name for a function called test in case 1. I can call this function and connect it to an event, making it not anonymous.

In case 2 however, you can see that the anonymous function, which doesn’t have a name, is still the same as Test in case 1. The reason it is formatted like that is because from what ever script this method calls the function if in the case, the occasion of part getting touched, it will function.

You can pretty much see this as in the example I provided earlier.

pseudo = fake. id thought it was understandable with template

i am tired mb, its 4 am and idk i wasnt that creative with a function name.
Basically a function which sole purpose is to return something iirc.

also pretty much what killerx9898 says

2 Likes

I think I understand it a lot better with this explanation. Appreciate it a lot

1 Like

Thanks for your words. I appreciate the advice. It did not really help THAT much but I understand that you’re tired and whatnot so I think you could be free and get some rest :smile:

Thanks for the effort

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.