LuaCow

The Cow Jumped Over the Moon.

Welcome to LuaCow, the fun interactive tutorial for learning how to program. This book is written for anyone who is entirely new to programming. Much like learning math, programming has very abstract ideas and it can be difficult getting started, I hope this book makes it a little easier for you. Just remember to stay curious and keep playing!

Why Lua?

Lua is very simple and very easy to learn. It is a great language to learn if you are new to programming, it contains much of the same ideas you’ll find in other languages so even if you’re thinking of learning a more complex language such as c++ or java I’d recommend you start with Lua first. So without further delay let’s get started.

Table of contents.

The very first thing.

The simplest thing we can program is printing some text to the screen. Now when I say print I don’t mean printing on paper, it just means displaying some text on your monitor.

print( "Hello internet!" )

This bit of code will print “Hello internet!” to your monitor.

Trying the code out.

This book is going to be interactive throughout. Where ever you see the black box it means you can enter code and run it. I recommend you play around with the code in each chapter, the more you play around the quicker you’ll learn.

Try printing your name, I’ve already told you what to type.

Ouput will be displayed here.

You should notice how it does not print the quotes around your name. The quotes tell the computer that you’re printing text, we call these strings. What are strings? This brings us to our next chapter.

Strings and numbers.

Lets define some basic definitions: A character is a single letter, number or punctuation, this includes spaces. Think of text as a string of characters, which is why we call them strings. However we can’t perform mathematic calculations on strings. “ab15” + “65 !” does not make any sense. We can however concatenate1 a string to join them together:

"ab15" .. "65 !" = "ab1565 !"

The double dot tells the computer to concatenate the two strings. If you want to perform mathematical calculations on numbers then you do not put quotes around the numbers.

15 + 65 - 10
19 * (100 - 5)
44 / 22

When you have math like this in your code, as the computer reads though each line, it will compute them for you and replace it with the calculation.

print( 1 + 2 )

Can you guess what this will print? Try it out and play around.

Ouput will be displayed here.

Variables.

Remember algebra? You may love it or hate it, probably hate it. However programming is a lot like algebra.

x = 5

Here we have a letter but without the quotes around so it isn’t a string, nor is it a number. We call this a variable, why? Because the meaning of x can vary. You choose the meaning of x and even after you’ve chosen you can change it later.

Think of a box with a label, we’ve happened to label this box x but we call label it cow if we wanted to, and we have put the value 5 into that box.

Ouput will be displayed here.

That is perfectly valid code, we can put whatever we like inside this box: numbers, strings and all the other good stuff you’ll be learning later. The code above will only print “Hello internet!” because each time we declare cow = something, we change what is in the box, not add to it.

Try making your own variables, don’t just label it cow, you can label it anything so long as it starts with a letter and doesn’t include any punctuation and spaces, except for underscores.

Variables ii.

Take a good look at the following code:

Ouput will be displayed here.

Now run it.

If you’ve been following you might be able to understand what’s happening, don’t worry if you don’t, i’ll explain.

Going back to the box analogy, we create two boxes labelled variable_a and variable_b and put the value 9 in one and 2 in the other. So what’s happening with variable_c = variable_a + variable_b?

This isn’t adding two boxes together, it’s adding the values inside these boxes together. That line is identical to variable_c = 9 + 2.

Get it? We’re putting the value 11 inside a box labelled variable_c and when we print it we get 11 printed to our screen. When we type the label of an existing variable we a referring to the value inside that variable.

variable_a = 1
variable_b = variable_a

This is not putting a box inside a box. We are copying the value inside variable_a into variable_b, variable_a still equals 1. The same applies to strings.

James = "tall"  -- Copy "tall" into the James box.
Lucy = James    -- Copy "tall" into the Lucy box.
James = "short" -- Change the James box to "short".
print( Lucy )   -- Print "tall".
print( James )  -- Print "short".

The double hyphen just indicates a comment for us humans to read, the computer ignores any text after a double hyphen.

Variables iii, in practice.

Lets use what we have learnt so far. Say you want an easy way to calculate the tax on an item.

If the item costs £150 and the VAT was 15% we might enter this into our calculator: ((15/100) * 150) + 150

If the cost or VAT changed we would have to rewrite our formula each time, with variables however:

Ouput will be displayed here.

Now it is very easy for us to just change itemCost and valueAddedTax and our program calculates the rest for us. Simples.

Functions.

A function is a section of code that has a specific function. In previous chapter we had a program that calculated the VAT on a price. Like a recipe, it has ingredients: itemCost and valueAddedTax and then it had a method of dealing with these ingredients.

netCost = ((valueAddedTax / 100) * itemCost) + itemCost

This is all good but if we wanted to calculate multiple items we would have to keep re-running the program changing the values each time. A function serves to shortcut this process.

Ouput will be displayed here.

Lets break this down. To declare a function we type function followed by the name of the function. Then between a pair of brackets we have a list of what look like variables. We actually call these arguments because they have no value, yet. Then we have our code that performs the calculation finished off by the word end that tells the computer this is the end of the function.

Now the computer won’t do anything with this function unless you call it. Take a look at the three calculateVAT. This runs the function and replaces the arguments, in the order that you’ve typed, with the values between the brackets. So calculateVAT( 100, 15 ) runs the calculateVAT function copying 100 into itemCost and copying 15 into valueAddedTax.

Have you noticed how print is also calling a function? We don’t see it but print has already been defined to print text to the screen.

function print( text )
    -- Do whatever to print text to screen.
end

My point here is you can call functions from within functions, you can even call the same function from within itself!2.

Functions ii, returning.

Let me introduce you to a new word return. As always it is best explained with an example.

Ouput will be displayed here.

Run the code and see the result. When we call a function it doesn’t just run the code inside, we have the ability to return values back to the point we called it from. In this case sumTwoNumbers(1,2) is replaced by 3 which makes result = 3. If we didn’t return anything result would equal nil3, literally it would tell us it doesn’t equal anything. Try it.

Functions iii, functions are variables.

There is another way you can declare functions that might make sense.

sumTwoNumbers = function(a, b) print( a + b ) end

We are creating a function with no name, called an anonymous function, and placing it inside a variable labelled sumTwoNumbers. Yes, functions are technically variables and they can be passed around like variables and even be passed into other functions.

Ouput will be displayed here.

Take a look at print( modifyText( prefixForename, someText ) ). We can nest function calls. Remember the computer will always calculate the innermost parenthesis pair first. Here’s what happens in steps:

  1. prefixForename is replaced with function(text) return "luke" .. text end. Note this function isn’t being run yet.
  2. someText is replaced with "hello".
  3. modifyText( function(text) return "luke" .. text end, "hello" ) is called.
  4. Computer jumps to the modifyText function.
  5. function(text) return "luke" .. text end is placed into anyFunction.
  6. "hello" is placed into text
  7. anyFunction is called with "hello" as the argument.
  8. anyFunction returns "lukehello".
  9. "lukehello" is placed into modifiedText.
  10. "lukehello!!" is placed into modifiedText.
  11. return "lukehello!!", computer jumps back to where it was in step 3.
  12. print( "lukehello!!" ) is called, printing lukehello!! to your screen.

You should now understand functions and why print( modifyText( prefixSurname, someText ) ) will print perkinhello!!

More to chapters to come.

I will be adding more chapters each week. I’d like to be able to keep this book going, however hosting this costs money. If you like what your reading and like to learn more then a small donation could really help keep this alive :)

Useful links to learn more.

Lua for beginners
Programming in Lua
Tutorial Directory, lots of other Lua tutorials!

About Luke.

Hello, I’m Luke Perkin the author of LuaCow. Seeing how popular Lua was getting I wanted to contribute my knowledge but in a way I would have liked to learn. I’ve been a curious programmer from an early age, however it is just a hobby.
Locofilm, My personal website


  1. Definition of concatenate.. ↩

  2. That’s called recursion and it has its practicalities. ↩

  3. nil is a special type for nothing, like the number zero we use it to indicate the lack of any value. ↩