Basics of Programming
In this thread, I'll try to tell you about variables, 'if then' checks, function and their parameters.
Although I might not be using the correct 'terms', this thread is just for explanatory purposes of how I think things work in Lua. Information in this thread can be 'questionable'.
Well, first I'd like to start off by explaining what's a variable. A variable is a place where you store a value, whether it's a string or number.
You can assign a value to the variable by using the '=' sign, for example:
Note: the term variable is what you want to refer to the variable by(you'll probably understand what I mean later on in the thread)
You can read variables and check it's contents.
Using a Stand-Alone Lua interpreter, that should print out 1. But what now? What's the use of assigning a value to a variable?
Well, first and most basic statement used in almost all languages is the if then statement. By reading a variable for it's contents and doing an action according to what it's holding.
Note: To evaluate a variable, you must use double '='s. You would only use 1 equal sign to assign a value to a variable.
This would print out 'text', or call for another function instead(we'll get to this part later on, for now, we'll just be using prints).
You can also act upon if the variable wasn't equal to 1, by using 'elseif'.
This would print 'even more text'. However, what if variable was equal to neither 1 or 2? You can use the 'else' statement here.
Note: 'else' statement should be at the bottom of all other checks.
This would print 'wtf not 1 or 2', because the variable was not equal to 1 or 2.
Alright, now to sum it all up, here's a commented example script:
Note: Comments must be preceed by '--'.
Functions and Parameters
A function is used to perform a task like print(text), prints text in the console window. Also, commonly, functions are used to simplify scripts(you'll understand what I mean later on in this thread).
I'll use an example I saw in a C++ tutorial... party invites.
Ofcourse, a program won't create the cards and walkdown the street to the postbox, but just for explaining
To invite someone to your party, you'll have to use these statements:
If you're inviting a friend or two, you won't have a problem.
But, what if you're invite 5 friends, or 10? Now you'll be crapping lines of repeated codes.
Ofcourse, you can create a function instead, making the script 'simpler' and easier to read.
Which one is easier to read, and faster to edit? Unless your insane and cross-eyed, I'm pretty sure I know what your answer is.
Now that you know what functions are used for, we get to the part on how functions are created.
First, we'll need to define the function itself.
example is the function's name, the name is used to refer to the function itself.
you can call the function by using it's name, which in this case is example.
Also, remember that you must add paranthesis, to tell the computer that you're calling for a function and not defining a variable.
I'll explain what goes between the paranthesis later on.
This would print out 'sometext' in console window everytime example() is used.
What if we would like to print out different text? In this case, we'll have to pass parameters to the function with the text we would like to print.
First, we'll need to define a new function that reads the parameter.
Now, we can call the function and tell it to print whatever text we want.
This would print out 2 different phrases, using the same function.
Now, for the last part of my thread...
You can also call a function from inside another.
First, the script would call for example(), printing out 'sometext'. Afterwards, the script would call for newExample, printing the parameter given to lastExample, which in this case, is 'some random text'.
Alright, hope I helped someone with this. Although it's pretty basic, I could've used something like this when I first started, maybe someone else would need it too.
Comments please.
In this thread, I'll try to tell you about variables, 'if then' checks, function and their parameters.
Although I might not be using the correct 'terms', this thread is just for explanatory purposes of how I think things work in Lua. Information in this thread can be 'questionable'.
Well, first I'd like to start off by explaining what's a variable. A variable is a place where you store a value, whether it's a string or number.
You can assign a value to the variable by using the '=' sign, for example:
Code:
variable = 1
You can read variables and check it's contents.
Code:
print(variable)
Well, first and most basic statement used in almost all languages is the if then statement. By reading a variable for it's contents and doing an action according to what it's holding.
Code:
if variable == 1 then
print('text')
end
This would print out 'text', or call for another function instead(we'll get to this part later on, for now, we'll just be using prints).
You can also act upon if the variable wasn't equal to 1, by using 'elseif'.
Code:
variable = 2
if variable == 1 then
print('text')
elseif variable == 2 then
print('even more text')
end
Code:
variable = 3
if variable == 1 then
print('text')
elseif variable == 2 then
print('even more text')
else
print('wtf not 1 or 2')
end
This would print 'wtf not 1 or 2', because the variable was not equal to 1 or 2.
Alright, now to sum it all up, here's a commented example script:
Code:
variable = 2 -- to assign a value to a variable, use just one equal sign and the value after it
if variable == 1 then -- evaluations to check a variable's value would require 2 equal signs
print('text')
elseif variable == 2 then -- seeing that variable is equal to 2(as assigned in first line), the script will execute the following block of code.
print('even more text')
else -- this print statement will not be executed, because variable was found to be one of the above options
print('wtf not 1 or 2')
end
Functions and Parameters
A function is used to perform a task like print(text), prints text in the console window. Also, commonly, functions are used to simplify scripts(you'll understand what I mean later on in this thread).
I'll use an example I saw in a C++ tutorial... party invites.
Ofcourse, a program won't create the cards and walkdown the street to the postbox, but just for explaining
To invite someone to your party, you'll have to use these statements:
Code:
Write [i]name[/i] on card.
Put card in envelope.
Write [i]name[i]'s address on envelope.
put envelope in postbox
But, what if you're invite 5 friends, or 10? Now you'll be crapping lines of repeated codes.
Code:
Write Pablol on card.
Put card in envelope.
Write Pablol's address on envelope.
put envelope in postbox
Write Colandus on card.
Put card in envelope.
Write Colandus's address on envelope.
put envelope in postbox
Write Zaga on card.
Put card in envelope.
Write Zaga's address on envelope.
put envelope in postbox
Write Helveg on card.
Put card in envelope.
Write Helveg's address on envelope.
put envelope in postbox
Write Kakan on card.
Put card in envelope.
Write Kakan's address on envelope.
put envelope in postbox
Code:
function inviteFriend(friend)
Write friend's name on card.
Put card in envelope.
Write friend's address on envelope.
put envelope in postbox
end
inviteFriend(Kakan)
inviteFriend(Colandus)
inviteFriend(Helveg)
inviteFriend(Zaga)
inviteFriend(Pablol)
Now that you know what functions are used for, we get to the part on how functions are created.
First, we'll need to define the function itself.
Code:
function example()
print('sometext')
end
you can call the function by using it's name, which in this case is example.
Also, remember that you must add paranthesis, to tell the computer that you're calling for a function and not defining a variable.
I'll explain what goes between the paranthesis later on.
Code:
example()
What if we would like to print out different text? In this case, we'll have to pass parameters to the function with the text we would like to print.
First, we'll need to define a new function that reads the parameter.
Code:
function newExample(textToPrint)
print(textToPrint)
end
Code:
newExample('sometext')
newExample('someothertext')
Now, for the last part of my thread...
You can also call a function from inside another.
Code:
function lastExample(text)
example()
newExample(text)
end
lastExample('some random text')
Alright, hope I helped someone with this. Although it's pretty basic, I could've used something like this when I first started, maybe someone else would need it too.
Comments please.
Last edited: