Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Lua Quick Start Guide

You're reading from   Lua Quick Start Guide The easiest way to learn Lua programming

Arrow left icon
Product type Paperback
Published in Jul 2018
Publisher Packt
ISBN-13 9781789343229
Length 202 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Gabor Szauer Gabor Szauer
Author Profile Icon Gabor Szauer
Gabor Szauer
Arrow right icon
View More author details
Toc

Scope

Like many other programming languages, Lua implements the concept of scope for anything that can be named (like a variable). A scope defines where in the program a variable can be used. Scopes are limited to the chunks they appear in. A chunk is just a section of code. Some languages call chunks blocks because they are represented by blocks of code.

Every Lua file that is executed is a chunk. This chunk can contain other, smaller chunks. Think of it as a hierarchical relationship. Such a relationship could be visualized as follows:

You can create a local chunk in a file by using the do keyword. The chunk ends with the end keyword. The following bit of code demonstrates how to create a local chunk in a file:

-- main file chunk is anywhere in the file

do
-- local chunk
end

do
-- a different local chunk
end

As mentioned earlier, scope refers to visibility. A chunk can access...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime