Learning Lua: Part 2: Functions and Tables

Learning Lua

Lua is a multi-paradigm scripting language originally designed to be embedded as part of other, existing programs. Often used for this purpose, Lua can be found in everything from photo editing applications to used as an internals scripting language of video games like World of Warcraft.


Functions

Screenshot 2018-11-02 13.05.01

In programming terminology, a function is a set of instructions. In Lua, like with using the if…then statement, a code chunk is within the function. When called (when the function is used), that chunk of code is run and then, if it encounters a return statement, it can result in one or more values being returned.

Like in other programming languages, functions in Lua also have parameters. Values can be sent into a function and, with the return statement, other values can be gotten out.

Functions End

Functions follow the same model as the if…then statement. A function is closed through using the keyword end. Just like the if…then statement, the code within the chunk is also indented by at least one space (although ‘tabbing’ is recommended).

Built-in Functions

One of the more common existing functions in Lua is print(). When used in a command-line environment, it “prints” data to the same place. Others like tostring() try to convert different data into a string and tonumber() that tries to convert other data like strings into numbers.

Tables

Lua does not directly support the data type of arrays found in many other programming languages. Instead, Lua uses a concept called a table. Similar to how other programming languages use key-value pairs to access data, Lua uses a table as a way to both sequence and map different values to each other.

Sequence of Values

Screenshot 2018-11-02 13.30.57

While arrays are not directly supported, much of the expected functionality of using an array can be done through using a table in Lua. For example, the common way of accessing the values within a sequence using their index and open and closed square brackets is possible in Lua. However, there is an important note for this: tables start with position 1.

Unlike in many other programming languages where the first position of an array is 0, in Lua, and when using tables, the first position is 1. When using index values, they begin with 1 and increase from there. Knowing their index values means that they can be accessed using the open and closed square bracket notation.

Mapping Values

Screenshot 2018-11-02 13.41.46

Tables in Lua can also be though of as sets of key-value pairs. Instead of using a value’s index within the table, it’s “key” can be used to access the data.

Table Functions

To help with tables, Lua also has a number of built-in functions. These include table.getn(), to get the number of entries that are not key-value pairs in a table; table.insert(), to insert a new value into a table; and table.sort(), to sort a table based either on their values or with an optional sorting function parameter.

Play with the example on Repl.it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s