Lua: Hello World
The first thing we're going to do is create a simple 'Hello World' program which outputs some basic text. It's extremely simple and uses only one line of code to output the text to the screen. Before I actually show you the line however, I'm just going to walk you through everything that you need to know to fully understand that line. After all, there is no real point in you writing a line of code that you don't understand!
A function, in Lua, is what we call a named piece of code. Luckily for us, Lua comes with a bunch of functions built in which can do basic stuff for us. We can do what is called calling a function in Lua by writing its name, and then by putting some brackets which may or may not have some values in. Calling a function essentially just executes the code inside of the function - so calling a function which outputs "Hello", would make the function output "Hello".
If a function doesn't need any values to do its job, then we can leave the brackets next to the function's name empty when we call it, but if a function requires some values (or a value) to do its job, we have to give it these values via a comma-separated list in the brackets. Any values that a function takes to do its job are called parameters.
Functions are usually made to take certain types of data - for example a function which outputs sentences will likely take what is called the string data-type as a parameter. The string data-type simply consists of multiple letters/characters, a simple sentence for example, and is shown in Lua by surrounding the data in double or single quotes. For example: "This is a string."
. We don't have to worry as much about data-types in Lua as some other languages, since the language does most of the hard work for us, but it's always something you should keep in mind.
So let's just say we had a function called 'print' which outputs text to the screen and it took a parameter of a string. In this example, let's just say that it outputs that string - that is the function's job. If we wanted to call the function with some data to make it output that data, we might write something like this:
1
|
|
In fact, this is the name of a function built into Lua, and this is exactly how we use it! The print function takes a string and outputs this to the screen, so in the case of the 'Hello World' application we were hoping to create, we could simply write something like this into a new '.lua' file and run the script:
1
|
|
See - Lua isn't that hard after all! If you've done any programming in the past, we're also allowed to put a semi-colon at the end of the line to show the line's end if we really want to, although a lot of people don't view this as "the Lua way" as this isn't necessary. My roots as a C programmer mess with me here, and so I often switch between having and not having the semi-colons (which isn't really a good idea, try to stick with one or the other!) - it's entirely up to you whether you think it looks better with or without:
1
|
|
It's worth noting that the print
function automatically puts a new line at the end of whatever you're printing out, so if we used the print function twice (by simply calling the function twice), there would be a line break between the two pieces of text:
1 2 |
|
If you don't want this behaviour when outputting text, you have a few options. You can either stick the two strings together before outputting them and only call the 'print' function once, or use a different function for the job!
If you want to stick the strings together, you are doing what is often called string concatenation - this basically just means joining strings together. An operator, in Lua, is just a symbol that does a certain job, and you can join strings in Lua by using the string concatenation operator between the two string. This operator is expressed as two full stops - ..
- so in our case, we could do something like this:
1
|
|
Even better than this for this example, because they are both constant strings - the values we've entered in the code aren't going to change when we run the script - we can simply put them both inside one set of double quotes:
1
|
|
The string concatenation operator can be useful though, especially when you aren't only dealing with string data-types, for example if we wanted to join a number to the string. The operator becomes even more useful when the concept of 'variables' is introduced, which is a concept which should become clear to you in the next few tutorials.
1
|
|
The other way I mentioned of avoiding the extra line-break in print
is using another function entirely. There is another function for outputting strings in Lua which is named io.write
(Input Output Write). This is essentially the same as print
, however it doesn't output a newline at the end of the string. For example, the below code will output on a single line:
1 2 |
|
As a side note, if you ever want to put a newline into a string, you can do it by writing \n
inside the double quotes. There are similar representations for other characters which are a bit stranger to write too, they usually begin with a backslash. If you're wondering how you'd just output "\n" without it meaning a new line, you could write "\\n" - this is called escaping a character, in this case we're escaping the '\'. An example of this functionality would be if we wanted 'print'-like behaviour, but with io.write
:
1 2 |
|