Variable

From ComputerCraft Wiki
Jump to: navigation, search
This has been marked for deletion!
The content of this page is likely obsolete, duplicitous or irrelevant. You should not use or trust all information you see here.
(Reason: This is an essentially a duplicate of Variables, just not formatted as well. Oeed 09:50, 25 March 2014 (GMT))

A variable is very important in any programming language. A variable can be used in very different ways, so you must know what kind of types there are.

First of all you must know what local does. If you use local before you define your example, then it can only be used in the current block and program. If you don't use it, then it will load globally, and then you can acces it in your other programs. But before you can use it in other programs, you must have run the program at least once per boot.

You can simply make a variable by putting a random name:

local random

To assign a value to it, we use the "=" symbol:

local random = 0

We have 5 different kinds of values:

- String
- Number
- Boolean
- Table
- Function

String

A string is something we use to print things. And like you should know, you use quotation marks when you are printing:

 print("Hello World!")

So when we want to assing a string to a variable, we still use the quotations:

local var = "Hello World"

Now we have this variable setup, we now can do:

print( var )

And that will output: Hello World!


number

We can also assign numbers to variables. This can be done like you saw previously:

 local random = 0

We can also do some maths using variables:

 local random = 2 * 3

If we now do the following:

 print( random )

Then that is going to output 6. So it executes the calculation, if the symbols are valid. Take a look at this page for more math functions.


Boolean

The shortened term for boolean is bool. A bool can only be true or false, this is very handy in if-statements. So to create it:

 local newBool = true

Remember, you can call your variable what you want.


Table

We assign a variable to a table. A quick example:

 local t = {}

Here is more information about tables.


Function

This is also a variable. You have 2 different ways of making a function:

 local printStuff = function( arguments )
   print( arguments )
 end
 
 local function printStuff( arguments )
   print( arguments )
 end

Here is more information about functions.

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox