2

All I learned about programming I learned by following youtube tutorials, or google, and some courses in edX, udemy. As far I understood variables are used for store values, string, etc, so i'm confused, to declare a local variable in lua, the syntax is, for example:

local myVar = 10

I know in other languages like JS, functions can be declared inside variables examples:

var myFunc = function() { 
  // ...
};

But I dont understand in lua:

local function myFunc()
   // ...
end

what is this used for?

Here a better example in a picture: code example , is a script for MTA:SA but thats irrelevant

3 Answers3

2

Variables

In Lua variables can be in two major scopes: global and local (let's skip table variables for now for clarity). To define a variable in the local scope you simply:

local my_variable = 5

By "local scope" we usually mean something like "the block this code is in". For instance: a local variable inside a function block would be available only inside that function. Or: a local variable at the top-level of the file is only available in that particular file.

You usually assign a value right away, but sometimes you may want to simply state that "I want my_variable to be in the local scope, but I don't know what it is just yet":

local my_variable

Then, assuming that you are in the same scope as before you can assign a value to it:

local my_variable
-- Some things happen, but we are in the same scope...
my_variable = 5

This will assign the value 5 to the my_variable in the local scope.

In case we wouldn't have the local my_variable first, then this statement would assign the value 5 to a global my_variable. Don't worry, this can be confusing at start. I recommend to simply play around with this idea by writing some functions, loops, and declaring, defining, then changing variables inside of them, with and without the local. This way you should be able to build up your intuition about the scopes more easily than reading raw descriptions.

You can also check out chapters in Programming in Lua: 4.1 - Assignment and the following 4.2 - Local Variables and Blocks.

Functions

As for functions, they are treated exactly the same way as any other value:

function my_func ()
end

Is a shorthand for assigning a "function as value" to variable my_func:

my_func = function () end

Now, we can declare my_func as a local variable just like we did before with my_variable. This would mean that the variable holding the function is only available in that particular local scope. The definition you wrote:

local function my_func () end

Is exactly that - a shorthand to define a function in local scope which expands to:

local my_func
my_func = function () end

For more technical descriptions you can check out Lua's reference manual:

Aki
  • 2,818
  • 1
  • 15
  • 23
1
local myFunc = function()
end

is the same as

local myFunc
function myFunc()
end

and

local myFunc = function()
end

is the same as

local myFunc
myFunc = function()
end

The first two are both function declarations, the third is a function declaration followed by an assignment.

Aki
  • 2,818
  • 1
  • 15
  • 23
0

In Lua, when you write:

local function myFunc()
    --...
end

It is essentially the same thing as:

local myFunc = function()
    --...
end

In the same manner, the following:

function myFunc()
    --...
end

Is the same as:

myFunc = function()
    --...
end

It's simply a shortcut for variable declaration. That's because in Lua, functions are first class objects, there is no special place where declared functions are stored, they are held in variables the same as any other data type.

Caveat

It's worth noting that there is a very small difference in behavior when using local function myFunc() instead of local myFunc = function().

When you declare the function using the former syntax, code inside the function has access to the variable myFunc, so the function can refer to itself. With the latter syntax, accessing myFunc inside of myFunc will return nil - it's not in scope.

So that means the following code:

local function myFunc()
    --...
end

Is actually more accurately represented as:

local myFunc
myFunc = function()
    --..
end

This is a small difference, but may be worth keeping in mind e.g. if you need to write a recursive function.

rosemash
  • 274
  • 1
  • 7
  • First you say that `local function foo() end` is `local foo = function () end` and then you say that it is `local foo; foo = function () end`. As you noted yourself this contradicts one another and creates different behaviour. This makes the answer confusing and misleading as only the second explanation is correct. First form is explicitly used in order to overwrite built-in function in local scope while still being able to refer to it within the new function scope. – Aki Jun 10 '22 at 07:49
  • @Green I see your point, but I assert that the first explanation is sufficient as an initial explanation to David Escobar's question, especially for those who aren't familiar with the specifics of Lua variable scoping. That is why I take care to say "essentially the same as". Then the caveat at the bottom to clarify, which has a large enough header that nobody will miss it. I believe presenting the information in 2 chunks makes it easier to follow, especially because the question seems to be a beginner asking about what function declaration syntax does in general. – rosemash Jun 10 '22 at 08:42
  • @Green I don't see the first answer as wrong. We basically explain two concepts: first the assignment of functions to variables, and secondly the differences in variable scoping provided by the different syntaxes. Since these are different concepts, and the asker's question was on the first concept, I feel more comfortable explaining them sequentially, with the first concept answering the question directly and the second concept building on the first one. – rosemash Jun 10 '22 at 09:22
  • Beginners do not understand the concept of variable shadowing. – rosemash Jun 12 '22 at 00:57