Closing functions, non-global functions, and the tail call of functions in Lua _lua

Source: Internet
Author: User
Tags lua

In the previous article we briefly introduced the LUA function, and this time, let's do something special to introduce the LUA function (Jor: Wait, did I miss something?) )

1. Closed function (closure)

In theory, all of Lua's functions should be called closed functions, but this is an anti-human approach, so let's just ditch it.

A closed function, as described in the book, is a function plus all "non-local variables" that the function needs to access.

Theory or something, very annoying, to look at a function:

Copy Code code as follows:

function count ()
Local i = 0;
return function ()
i = i + 1;
return i;
End
End

The Count function returns another function, and the point is that the returned function uses the local variable of the Count function.

First run, look at the effect, use the following method to invoke:

Copy Code code as follows:

Local func = count ();
Print (func ());
Print (func ());
Print (func ());

The output results are as follows:

Copy Code code as follows:

[Lua-print] 1
[Lua-print] 2
[Lua-print] 3

How can the narration not come out to growl? (Jor: ...) Cough, cough ... For the yarn to be like this?! should not all output 1?! )

Here, local I belongs to a non-local variable because it is neither a global variable nor a simple local variable (because another function can ask it).

To go back to the definition, the function in the Count function, plus the non-local variable i, constitutes a closed function, as simple as that.

For a closed function, a non-local variable that belongs to it is not generated temporarily when it is invoked, but exists with it.

So each time the closed function is invoked, the value of the nonlocal variable is not reset.

If you're still not sure, then let's add a local variable to the closed function and modify the Count function as follows:

Copy Code code as follows:

function count ()
return function ()
Local i = 999;
i = i + 1;
return i;
End
End

This time, I as a local variable of the internal function, it is no longer a "nonlocal variable."
Still call like this:

Copy Code code as follows:

Local func = count ();
Print (func ());
Print (func ());
Print (func ());

The output results are as follows:

Copy Code code as follows:

[Lua-print] 1000
[Lua-print] 1000
[Lua-print] 1000

Fall into, each time the value of the I variable is all new.
The use of closed functions can be large, we are in the development process of the frequency should be relatively large ~

2. Non-global functions

It is this kind of looking at the anti-human noun, not the global, that is, not the global function (Jor: waste you a word Ah!) )

Let's look at a function like this:

Copy Code code as follows:

Local function Mutou ()

End

This is a non global function, but, such a simple thing I would not come up to say ~

Here I would like to introduce a "grammar sugar", the Mutou function above, in fact, equivalent to the following code:

Copy Code code as follows:

Local Mutou;
Mutou = function ()

End

This is the true face of our function, and the function name is actually a variable name.
So, sometimes, when we're defining functions, we need to be aware of the order. For example, two functions like this:

Copy Code code as follows:

Local function Mutou ()
Print ("Mutou");
return Pangbai ();
End
Local function Pangbai ()
Print ("Pangbai");
End

In the Mutou function, you call the Pangbai function.
At compile time, the Mutou function is not compiled, because the Pangbai function is undefined at this time, in other words, the Pangbai variable does not exist.
You can solve this problem in a different way:

Copy Code code as follows:

Local Mutou;
Local Pangbai;
Mutou = function ()
Print ("Mutou");
return Pangbai ();
End
Pangbai = function ()
Print ("Pangbai");
End

So the compilation is no problem, well, perhaps Lua should not be called compiled, anyway, will not report grammatical errors ~

3. Tail call

Well, a noun is more against mankind than a noun.

The general meaning of the tail call is that when a function is called the last action of another function, this call is called a tail call.

For example, a function like this:

Copy Code code as follows:

function Mutou ()
return count ();
End

When the Mutou function calls the Count function, there is nothing else to do, so calling the Count function belongs to the tail call.
However, if this is the function:

Copy Code code as follows:

function Mutou ()
return count () + 1;
End

This is not part of the tail call, because after you call the Count function, you also get the return value of count, and then do an addition operation, which does not fit the definition.

What's the point of the tail call?
The tail call does not consume extra stack space, such as a classic function:

Copy Code code as follows:

function foo (n)
If n > 0 Then
return foo (n-1);
Else
Return "End";
End
End

We can try this call:

Copy Code code as follows:

Print (foo (99999));


The result is an expected output of the end string.
When n > 0 o'clock, the function returns to Foo (n–1) directly, then there is no subsequent action, so this conforms to the definition of the tail call.
Therefore, the invocation of this function does not cause a stack overflow.

But, if you change it a little, it becomes this:
Copy Code code as follows:

function foo (n)
If n > 0 Then
return foo (n-1) + 0;
Else
Return "End";
End
End

When running, you'll report an error like this: [string "Src/main.lua"]:57:stack Overflow

Since I'm not using LUA, I'm not going to give you a more practical example, but it will be a very useful feature.

Well, for the LUA function, it should all be introduced, and I am also in the order of the book to go through the basics, and then the interesting part of the article is recorded.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.