Update CrashCourseInLua.rst

gccjit-ravi534
Dibyendu Majumdar 7 years ago committed by GitHub
parent ac3f96d0fe
commit 8da099c557

@ -45,25 +45,28 @@ Lua is dynamically typed
========================
This means that values have types but variables do not. Example::
x = 1 -- x is integer
x = 5.0 -- x is now a double
x = {} -- x is now an empty table
x = function() end -- x is now a function with empty body
x = 1 -- x holds an integer
x = 5.0 -- x now holds a double
x = {} -- x now holds an empty table
x = function() end -- x now holds a function with empty body
Variables are global unless declared local
==========================================
In the example above, `x` is global.
In the example above, ``x`` is global.
But saying::
local x = 1
makes `x` local, i.e. its scope and visiability is constrained to the enclosing block of code, and any nested blocks. Note that
makes ``x`` local, i.e. its scope and visiability is constrained to the enclosing block of code, and any nested blocks. Note that
local variables avoid a lookup in the 'global' table and hence are more efficient. Thus it is common practice to cache values in
local variable. For example, math.abs() is a function - and following creates a local variable that caches it::
local variable. For example, ``math.abs()`` is a function - and following creates a local variable that caches it::
local abs = math.abs
abs(5.5) -- invoked same function as math.abs
local print = print -- caches global print() function
print('hello world!') -- calls the same function as global print()

Loading…
Cancel
Save