Any advantage in declaring a function as local?

Is there any advantage to declaring a function as local?

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.

Any advantage in declaring a function as local?

2 Likes

The only benefit I see to it, is that it organised the stack better

Lua has no concept of “local functions”.

When you do

local function f()

end

you are actually doing

local f
f = function()

end

the former is syntactic sugar (a nicer way to write something) for the latter.

Do you know why you use local variables? That is exactly why.

3 Likes

In short, the only advantage of declaring a function as local is if I intentionally want to restrict it to a scope. For the rest, it is only a disadvantage …

What? How is using local a “disadvantage”? You seem to be severely misinformed.

Global variables are bad practice.

if true then
    x = 5
end

x = 10 -- OOPS, we are writing to the same variable declared in the if block!

It makes you more prone to name collisions and it overall pollutes the environment.

Also: while negligible, local access is faster than global access. The reason for this is locals are added to the stack. Whereas globals in the environment.

1 Like

I understand when they say that using Global is not a good practice.
However, there are several statements in the world of programming that are not absolute, such as the derision that was made with the use of goto, which has been abolished from most programming languages, however very useful in several cases of more complex logic.

The same is true of the “Global or Local” dilemma.
As I stated, as far as I understand it, the only advantage of declaring a function as local is for the scope constraint, but with that I lose flexibility and gain hassles.

It would be better to have a benchmark that really proves the difference in processing with the use of local vs global.
In this way we would not be just in the belief but we would be sure of the use or not of globals.

1 Like

what “flexibility” do you lose? and what “hassles” do you gain?

How do you suggest we do that? You don’t even use locals because they are faster to access, you use them because it’s just proper coding style.

1 Like

Global functions can be declared anywhere in the code. Local functions must always be declared before, which forces me to constantly change the position of functions in the code, according to the need for them to be used in different parts of the program.

1 Like

Well I think your issue is implementation. Why are you trying to call a function before it exists.

1 Like

Flexibility. I don’t have to worry about where the function is declared inside the code.

1 Like

That is not flexibility. You should know what your functions do beforehand before you call them.

You did not understand. I know what my functions does.
But when the function is declared as Local, I am obliged to always place it at the top of the code, in the first few lines.
When the function is not local, I have the flexibility to declare it more in any part of the code.

Example:

function func2()
	func1()
end

function func1()
	print('OK')
end

func2() -- works
function func2()
	func1()
end

local function func1()
	print('OK')
end

func2() -- don't work

To keep func1() as local I have to move it before func2():

local function func1()
	print('OK')
end

local function func2()
	func1()
end

func2() -- works

Now imagine that having to be done constantly in a large code, I always have to rearrange the functions within the code, just because they are declared as local

Which is a good thing! That means everything is in order and you know what it does beforehand.

For your second example I had to look down to see what it did. That is not good.

Now, in one thing you are right: local variables are faster. And I say this because I made a benchmark using this code.

From the results I tested, I can assert:

local variables are processed 3 to 4 times FASTER than global variables

I believe local variables are always better than globals, but you are right that this restraint on a function’s location is oftentimes terrible for code organization.

Say you have 4 functions, each implemented in the last:

function findInTable() ...

function indexTable() ...

function validateIndex() ...

function decodeIndex() ...

It’s more of a pain to scroll down to find the main functions at the bottom than it is to simply glance at them right at the top. i.e, you find the findInTable function at the bottom, but a new reader (like future you from 2 years in the future) would be confused as to why s/he is finding all these technical functions like decodeIndex surfacing up top. Unless, you still have a trained instinct to scroll straight down to the bottom, unlike me.

The usual answer in Lua is “tough,” but you can use tables to stay local while receiving the benefits of globals at the same time:

local TableLib = {}

function TableLib.Find() ...

function TableLib._index() ...

function TableLib._validateIndex() ...

function TableLib._decodeIndex() ...

Accessing these functions are probably just as fast as accessing a global, if not slower, but I don’t really know.

Also, having to keep reading down to find the main functions and know what they do is time-consuming and uses up the mental power you need to move on to the next feature or fix the next bug. It is much more professional (in my opinion) to be able to assume what your code does and trust that it works through clear and concise function/parameter names and without having to understand lower-level implementations. If you need more information, you keep reading down, and if you only need a gist, it’s right there.

1 Like

When your declaring variables or functions its is always bet to have them at the top of the script, current control statement, loop or function anyways, so I don’t see that as a disadvantage.

You are all insisting that having a function declared at the beginning of the program is easier for understanding a code and that “scroll down” the code to find the function is worst.
This may even be true for small code.
But that makes no difference in a code with THOUSANDS OF LINES, with HUNDREDS OF FUNCTIONS. In this case, you will not “scroll down”, you will always have to search for the function via Ctrl+F.
Hence, it makes no difference whether the function is above or below in the program.
But it is rather a disadvantage when you try to run the code and Studio warns you that the function does not exist just because you declared it as local and then you have to move it through the code until you find an ideal line for the function to be found by the compiler.
Then you discover that there were other functions that depended on this function that you moved, so you have to rearrange these other functions as well, and so on. A nightmare!

1 Like

Store functions in modules so you can just require them at the top of your script and call them accordingly. Fortunately this forces you to have your functions modular which means no global variables.

3 Likes

I believe it’s just personal preference. I always declare my functions as local since if I needed a global function I would prefer to use a Module Script and require that. But as @rogeriodec_games has mentioned in their previous reply their can me drawbacks for some people.

All I can say is local functions can guide people into cleaner code. Since you will always have your functions above your main loop or event logic.

1 Like

Local functions can only be used in one script, while normal functions (Global Functions) can be used from any script in the game.

Within the one defined as local function, foo (its name) can be accessed as an upvalue for recursion, while in the local foo = definition , it can not.

If you’re just planning to use it in one script, go for local functions.