Any advantage in declaring a function as local?

Really? I’m pretty sure a global scope is only ‘global’ through that script only.

Render()--<Error
Renderv1()--<Will work
local function Render()
    print("Rendering");
end;
--
function Renderv1()
    print("Rendering");
end;

For global variables/functions across scripts, you’ll need to use _G

2 Likes

Yeah, it’s only ‘global’ insofar as it’s accessible from that specific script, which is why I find it kind of pointless to argue about imo. Functionally they’re nearly identical, and while there may be some performance advantages in using local functions, I would guess that for 90% of use cases it won’t be big enough to make a difference. For most use cases, at least with Roblox, it’s gonna come down to a preference and how you’re organizing your code.

2 Likes

Probably meant any block in the script, unless you mean defining functions globally as in literally globally in a shared environment.

Defining a function through syntactic sugar:

local function foo() end
-- is the same as doing
local foo; foo = function() end

and defining functions locally makes them faster to access since the local is added to the stack.

1 Like

That is another issue your touching on, if your code spans thousands of lines, your more than likely doing too many things in that single script/module. imo no source file should be more than 1000- 1500 lines long.

Why? What’s the difference between 1,500 and 15,000 lines? From 500 lines long, in my opinion, it is already difficult to move through the code without using bookmarks, collapsing, Ctr+F, and other tools (that’s why I use Sublime Text 3 and not Studio’s native editor).
So, no matter if you have 1,500 or 15,000 lines, the job will be the same.

Yeah of-course it will do the same thing but it will make your code terrible to maintain.

Why? A single script’s strategy is highly recommended by many developers. In this case, I’ll have only 3 scripts: a LocalScript, a ServerScript and a ModuleScript.
I think it is much easier to maintain the code in this way than to distribute it unnecessarily in several scripts, which, on the contrary, makes maintenance more difficult.

Is there any advantage to declaring a function as local ?

Yes. They are faster to access, and you avoid polluting the global* namespace. Here is some info about the global environment from PIL.

For simple programs, honestly, it probably doesn’t matter what you do. As your programs get more complex, though, you will start to realize the benefits of well-defined scope.

Because so far I’ve only seen disadvantages : without local , the function can be located anywhere in the code and when declared local , the function must always be above the line that calls it.

This is false. The following fails:

printHello() -- attempt to call global 'printHello' (a nil value)

function printHello()
    print("Hello")
end

You may be referring to something like this:

workspace.Part.Touched:Connect(function()
    printHello()
end)

function printHello()
    print("Hello")
end

This works because when you declare something without local, you are actually putting it in a table that is shared for the entire script*. The printHello() call triggers a lookup in that global* table.

However, that’s not a unique thing to globals, it’s more of a side-effect of the way lua is evaluated (at runtime). This works too, but makes printHello a local function:

local printHello

workspace.Part.Touched:Connect(function()
    printHello()
end)

function printHello() -- same as  printHello = function()
    print("Hello")
end

You still need the forward declaration at the top, since you’re not storing the variable in the global* table, but you can put the definition wherever you want.

*: in non-roblox Lua, every non-local variable is put in _G and shared among all modules in the program – not true for ROBLOX. In ROBLOX Lua, each script has its own “global” table that other scripts cannot access.

3 Likes