LUA Basics (5) Variables in--lua

Source: Internet
Author: User
Tags lua
Introduction

In Lua, a variable must be declared in code before it is used, that is, the variable is created. This is because the compiler needs to know how to open a store for a statement variable before the compiler executes the code to store the value of the variable. Variable Type

There are three types of variables in LUA: Global variables, local variables, and fields in the table.

The variables in LUA are all global variables, even in blocks of statements or in functions, unless declared as local variables with the local display. Scope of the variable

A local variable is scoped to the end of the statement block from the start of the declaration position. When another module is called, only the global variables that are in that block of the statement cannot access the local variables inside.

The default value for a variable is nil.
Code Show

A = 5               --global variable local
b = 5         --local variable

function joke ()
    C = 5           --global variable local
    d = 6     --local variable 
 
  end

joke ()
print (c,d)-          5 nil

do 
    local A = 6     --local variable
    b = 6           --Global variable
    Print (A, b);
--6 6 End

print (A, b) and      5 6
 

Perform the above instance output as:





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.