Functions

Functions are, most simply described as blocks of code you can run at a point in the future, which is called "calling" a function.

Declaring a function is simple, and an example is to the right. Simple put "function" before the name of the function, then, in parentesis after that name, the names of any arguments for that function seperated by commas. Arguments are simply local variables created when the function is called, an example of one would be the text you give to print(). You are also able to create a function with no arguments as well.

While a function on its own is sort of useful, you can also do something really nice with them, and that's add a return value. A return value simply makes it so that, when a function is called, it "returns" a certain value specified in the function. Practically, this means that, if you used a function in place of a variable somewhere, the function would act like a variable with a value the same as what it returns. An example of a function with a return value is to the right.

Also, it should be noted you can create local variables inside functions, and that variable will only exist for that function and any statements within that function.

Also also, functions are sort-of treated like variables, but you are able to create a variable and a function with the same name. For example, you could have a variable named "foo" and a function named "foo" exist at the same time. This works because, in any situation where you'd call a function, you'd put parenthesis after it to call it. Of course, you still shouldn't do this, as it could cause some confusion.

And that's about it for both this lesson and the tutorial all together. For further reading, see the reference manual for Lua 5.4, and the Lua Users Wiki. Both are helpful guides for various things. The reference manual is, of course, a reference manual for people who know quite a bit about programming already, so I recommend the Wiki more.

In any case, have a nice day!

functon foo(bar1, bar2)
--code here
end

functon add(var)
return var + 1
end