-- defines a factorial function
function fact (n)
if n == 0 then
return 1
else
return n * fact(n - 1)
end
end
print("enter a number:")
a = io.read("*n") -- reads a number
print(fact(a))-
each piece of code that Lua executes, such as a file or a single line in interactive mode is called chunk.
-
Lua is used also as a data-description language, chunks with several megabytes are not uncommon.
-
call the function
os.exit(), from the Operating System library to exit interactive mode. -
In older versions (< 5.3), we need to precede expression with an equal sign to print it.
a = 15
= a^2 --> 225.0
lua -i prog
-
Start interactive session after running a given chunk.
-
Another way to run chunks is with the function
dofile, which immediately executes a file.
--- lib1.lua
function norm (x, y)
return math.sqrt(x^2 + y^2)
end
function twice (x)
return 2.0 * x
end> dofile("lib1.lua") --load library
> n = norm(3.4, 1.0)- Comment starts anywhere with two consecutive hyphens (--). Long comments with two hyphens followed by two opening square brackets and run until the first occurance of two consecutive closing square brackets.
--[[multi-line long comment ]]
- Lua is a dynamically-typed language.
-
userdataare used to represent new types created by an application program or a library written in C; for instance -
Booleans do not hold a monopoly of condition values: in Lua, any value can represent a condition.
-
Lua considers both zero and the empty string as true in conditional tests. Only Boolean false and nil are considered as false and anything else as true.
-
logical operator
and,orandnot. -
The result of the
andoperator is its first operand if that operand is false; otherwise, the result is its second operand. -
The result of the
oroperator is its first operand if it is not false; otherwise, the result is its second operand. -
A useful Lua idiom is
x = x or v, which is equivalent toif not x then x = v end. sets x to a default value v when x is not set (provided that x is not set to false). -
((a and b) or c)or simply(a and b or c)is equivalent to the C expressiona ? b : c, provided that b is not false. -
The
notoperator always gives a Boolean value -
The
-eoption allows us to enter code directly into the command line, like here:
% lua -e "print(math.sin(12))"
- A script can retrieve its arguments through the predefined global variable
arg. A script can also retrieve its arguments through avarargexpression. In the main body of a script, the expression...(three dots) results in the arguments to the script.

