Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
4

What's the Difference between "function Event()" and "Event=function()"? [closed]

Asked by
woodengop 1134 Moderation Voter
9 years ago

I've seen some scripts with Variable=function()what difference does it have between function Name()?

2
There isn't really any difference, however, setting the function up as a Variable in a Table allows you to use it in, well, a table (Example: local Table = {Var = function() print("Yay!") end)}), and you can call it still (Example: Table.Var()). TheeDeathCaster 2368 — 9y
0
ohh...So basically it both can be used? woodengop 1134 — 9y
1
Yes, it's just that the Variable version has a few more advantages, but can sometimes error when not used correctly. TheeDeathCaster 2368 — 9y
1
It's just the style that people write code in. EzraNehemiah_TF2 3552 — 9y
View all comments (2 more)
0
ikr woodengop 1134 — 9y
0
function Name() is the preferred method i believe, so use it whenever possible. ZeptixBlade 215 — 9y

Locked by woodengop, alphawolvess, and adark

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
9 years ago

Your question has been adequately answered already, but to remove it from the Unanswered sort category, I'm going to a write a full answer.

There is absolutely no difference between:

function funcName() end

and:

funcName = function() end

In fact, the former is actually just syntactic sugar for the latter!

When using Tables, either form works except for once case:

local tab = {}

function tab.funcName() end --valid
tab.funcName2 = function() end --valid
tab["funcName3"] = function() end --valid (Also lets you make functions with non-string identifiers)

function tab["funcName4"]() end --INVALID
    --This is the only case where the sugar doesn't work. I'm not sure *why* it doesn't work, but I've tested it before and it provably does not work.
0
Cool! Thanks adark! woodengop 1134 — 9y
Ad