Several special aspects of functions in Lua explore _lua

Source: Internet
Author: User
Tags lua

I did not think that the distance from a basic supplement has been more than 1 years, and recently ready to pick up the LUA, the foundation to fill up, today to talk about the LUA function bar ~

0. Environmental

The main reason for my sudden interest in Lua is that the Cocos Code IDE is starting to surface, a cocos2d-x official Cocos2d-x+lua or JS-specific IDE, trying to use it, though not perfect, but it's worth looking forward to.

So, the LUA editor used in this article chooses it, so let's just be random.

1. Function of Literacy--lua

Lua is very simple to create and invoke functions, such as code:

Copy Code code as follows:

function Mutou ()
Print ("This is definitely not advertising: www.jb51.net");
End

This creates a function, and calling the function is simple:
Copy Code code as follows:

Mutou ();

Then run, you can see the output, print is a function, alas, no more nagging.

2. Literacy-the parameters of the function

Just the Mutou function, we modify the parameters, such as code:

Copy Code code as follows:

function Mutou (name, age)
print (name);
print (age);
End

We add two parameters to Mutou, one for name, one for age, and the other for ages.
By the way, LUA has no variable type, and a variable will show a different type depending on the value given.

Let's try this call the Mutou function:

Copy Code code as follows:

Mutou ("Wood", 25);

Then run the output as follows:
Copy Code code as follows:

[Lua-print] Wood
[Lua-print] 25

Very normal, I like it ~

3. Massive heart--the function parameter number adaptively

Lua's function is broad, and all the arguments we pass in will endure, but it will be filtered according to its rules.

To continue just that Mutou function, let's try this call:

Copy Code code as follows:

Mutou ("Wood");

One less parameter, but this is not the same as C + + or Java, which does not cause compilation errors and can still function correctly. The output is as follows:
Copy Code code as follows:

[Lua-print] Wood
[Lua-print] Nil

For missing parameters, it is automatically replaced with nil.

And then, since you can give fewer parameters, then naturally you can give more parameters, try this call:
Copy Code code as follows:

Mutou ("Wood", 25, "narration", 45);

(Jor: You're only 45 years old!) Your entire cell is 45 years old! )
The output is as follows:
Copy Code code as follows:

[Lua-print] Wood
[Lua-print] 25

As a result, more parameters will be shed.
As to whether this is a good thing or a bad thing, it depends on who is using the ~

4. Multiple return values

Yes, Lua seems to think of all the things we want to be lazy about, and it's done.

The LUA function supports returning multiple values, continuing to modify the Mutou function as follows:

Copy Code code as follows:

function Mutou (name, age)
return name, age;
End

To return the two parameters that were passed in, try this call:
Copy Code code as follows:

Local name = Mutou ("Wood", 25);
print (name);

Call the Mutou function and save the return value with a variable, and the output is:
Copy Code code as follows:

[Lua-print] Wood


Because we only use a variable to hold the return value, so even if the function returns two values, we can only get one, to get two values, it is very simple, as follows:
Copy Code code as follows:

Local name, age = Mutou ("Wood", 25);
Print (name.. ":" .. Age);

Enter the results as follows:
Copy Code code as follows:

[Lua-print] Wood: 25

By the way, use the symbol "..." Can connect string, here is not much to say ~

Return values and parameters, if we try to get a function return value with three variables, it does not cause a compilation error, as follows:
Copy Code code as follows:

Local name, age, hehe = Mutou ("Wood", 25);
Print (name.. ":" .. Age);
print (hehe);

Enter the following:
Copy Code code as follows:

[Lua-print] Wood: 25
[Lua-print] Nil

For extra variables, nil values are given, although this does not cause compilation errors, but it can also cause run-time errors.

For example, use the ".." Symbol to connect the nil value, you will get an error.

5. Characteristics of multiple return values 1--must be the last element of an expression

For functions that have multiple return values, it is not always possible to return multiple values in all cases.

Let's take a look at this particular case, which is still just the Mutou function, which we call:

Copy Code code as follows:

Local A, B, C = "A", Mutou ("Wood", 25);
Print (a);
Print (b);
print (c);

The output results are as follows:
Copy Code code as follows:

[Lua-print]
[Lua-print] Wood
[Lua-print] 25

Yes, the LUA assignment statement supports a comma expression (that is, assigning multiple values to multiple variables), and the first value is naturally assigned to the variable, so the value of the variable A is the one.
However, this is not the point ~ (Jor: Then you say a yarn ah!) )

The point is, we'll switch the order, as follows:
Copy Code code as follows:

Local A, B, C = Mutou ("Wood"), "a";
Print (a);
Print (b);
print (c);

The output will be unexpected:
Copy Code code as follows:

[Lua-print] Wood
[Lua-print]
[Lua-print] Nil

Yes, the return value of the Mutou function becomes only one, which is one of the features of the function: if the function's invocation is not the last element of the expression, then only one value is returned.

What the? Isn't it clear enough? Then let's raise a particle (Jor: particle you!) )
So we call the function ... Well, that, let's move on to the next item. (Jor: Don't give an example.) You were just so excited about the yarn! )

6. Characteristics of multiple return values 2--Force returns a value

In some cases, we may be bored with a function, yes, why do you always return multiple values?! (Jor: You're not writing your own function yet!) )

If you want to force a function to return only one value, you can do this:

Copy Code code as follows:

Local A, B = (Mutou ("Wood", 25));
Print (a);
Print (b);

Enter the results as follows:
Copy Code code as follows:

[Lua-print] Wood
[Lua-print] Nil


We just need to put the function call in parentheses when the function is called.

7. Name parameter//named arguments

Finally, we look at a more practical function parameter--the name parameter.

In fact, "name parameters" is my own term, "LUA programming" in the second edition is called "with Name," but I see English words and usage, I still prefer to call it "moniker."

First to ask you a question, we have just been discussing the Mutou function, how many parameters, do not go back to see, 1 seconds to answer ~ (Jor: 2!) )

Second question, which is the age and name parameter of the Mutou function, which is the previous one? Answer in 0.001 seconds! (Jor: Answer your head!) )

Yes, sometimes we don't remember the order of the parameters of a function, is it the first argument, or is name the second argument? (Jor: It seems to be wrong ...) )

So, it comes in handy to have a reference.
Let's revise the Mutou function:

Copy Code code as follows:

function Mutou (args)
return args.name, Args.age;
End

Now the parameter of the Mutou function becomes a table, which requires the table to contain the name and age two values.

So, the way we call the Mutou function becomes this:
Copy Code code as follows:

Local args = {name = "Wood", age = 25};
Print (Mutou (args));

Yes, passing a table variable to the Mutou function can be, because the function in taking the parameter value, is based on the table key value to obtain, does not need to consider the parameter order, this solves the order we just said the problem.

8. End

Well, about the basic nature of the function, that's all, and of course, there's a variable-length argument I didn't mention it, because it didn't seem to be a good thing to mention.

One of the things I've been planning to do lately is that I don't write games at night, even if I get excited bugs.
I want to spend the evening to charge, my basic skills is not enough, a little shaky feeling.

The recent plan to "LUA programming" this book over again, hoping to adhere to ~
The plan is to spend an hour each night reading and finishing the tutorials for an hour.

Today is the first day, very tired, writing a tutorial than to write a game more tired.
There is a saying, never let yourself in the comfort of the range, well, stick to ...
(Jor: With the ellipsis of a yarn!) Use an exclamation point! It means you have the determination ... )

I said, little if, then why did you end up with the ellipsis ...

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.