Introduction Welcome Getting Started My First Program     Troubleshooting and Distributing Transforming Data Interactive Programs     Programming Exercises     Making Games
Documentation Declarations     Procedures     Variables Statements     Flow Control Expressions Libraries     Print     IO     Random     Game Engine
Tutorials Complete Tutorials     Introduction to Programming     Quick Start     Programming Exercises     Programming Games

# VARIABLES

## All About Variables in Vizzcode

 What are Variables and How to Create Them? A variable is an association of a name to a type-safe piece of memory. There are lengthy introductions on variables here as well in the complete tutorials if you need a beginner-friendly discussion on the topic. You can declare a variable this way: var : int; A variable is declared in text by typing a name followed by a colon : There could be an explicit or implicit type after that and also an assignment. var1 : int; var2 : int = 5; var3 := 5; In Vizzcode, all variables must have their type resolved at compilation time. This is known as statically-typed or strongly-typed. Variables can be global or only available in the scope they were declared at. Types In Vizzcode, variables can be of one for the following types: Integer: Whole numbers Float: Decimal (floating point) numbers String: Text Boolean: true or false In text code, you can specify types explicitly at variable declaration: var1 : int; var2 : float; var3 : string; var4 : bool; The same thing can be done in the Visual panel: The variable type can also be implicit (or auto, in the Visual panel) so that the compiler automatically resolves the type based on the expression the variable is being assigned to at declaration. The following sections explain each of Vizzcode's types in detail. Types: Integers In Vizzcode, integers are 64-bit signed whole numbers, ranging from −9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. (Yes, that's a lot of numbers) You can declare an integer variable by specifying the type int or leaving the type implicit and assigning it a whole number: int_var : int; another_int : int = 10; one_more := 20; If you declare an int variable but don't assign it a value, it's assigned by default to zero. You can use arithmetic expressions to operate on ints as well as comparison operations Types: Floats In Vizzcode, floats are types used to store decimal numbers. They are 32-bit single-precision floating point numbers. You can research more about the single-precision floating point format if you would like to know more about it. A float declaration can be written by specifying the type float or leaving the type implicit and assigning it a decimal number: float_var : float; another_float : float = 10; one_more := 10.0; A float value is initialized to zero if you don't assign it a value explicitly. You can use arithmetic expressions to operate on floats as well as comparison operations. Types: Bools The bool type is an 8-bit value that can hold either the value false or true. The value false is represented in memory by the value zero, and anything else is considered true. You can declare a boolean variable by specifying the type bool or leaving the type implicit and assigning it either a literal value of true or false (like in the following example), or an expressions that resolves to a bool: bool_var : bool; another_bool : bool = true; one_more := false; Bools are the result of comparison operations you can apply to other types. For example, comparing if an int is greater than another one results on a bool: either true or false. first_int := 5; second_int := 7; comparision_result : bool = first_int > second_int; If you declare a bool variable but don't assign a value to it, it's assigned by default to false. Bools can be operated on with special logical operators described in detail here. Types: Strings In Vizzcode the type string is used to store and operate on text. They are internally represented by a pointer to 8-bit integers storing characters and a 64-bit signed integer containing the count of characters in that string. You can declare a string by specifying the type string or leaving the type implicit and assigning it a string (either a literal text - literal texts are specified between double quotes "" - or an expression that resolves to a string - like a procedure call) string_var : string; another_string : string = "This is a literal string"; one_more := "Another nice text"; A string value is initialized to an empty string of text (or "" ) if you don't assign it a value explicitly. You can compare strings using these operators. Implicit Types For convenience, you may leave the type of a variable implicit when declaring variables. The compiler will then use the type of the expression assigned to it in its declaration to determine the type of the variable. In text code, you can just leave the type hidden in the declaration: var1 : int = 10; var2 : = 10; var3 := 10; Because the variable type has to be known at compile time, you can't declare a variable with no type and no expression. Implicit types work on compound declarations just the same. In fact, if you are receiving multiple return values of different types, you are required to use to use implicit type because you are creating variables of multiple different types in the same declaration. proc1 :: () -> int, int {} proc2 :: () -> int, float {} main :: () {     this_is_an_int1, this_is_an_int2 : int = proc1();     this_is_an_int3, this_is_a_float : float = proc2(); } The following section discusses how to leave the type implicit in the Visual Panel. Node Type Auto As discussed in the previous section, you don't need to specify every variable's type at declaration: you can just let the compiler automatically figure out the type based on the value of the expression. You can do this by just specifying the type to auto in the variable declaration node: Because the compiler needs an expression to resolve the variable's type, you must specify a value in the variable's declaration when using the type auto. Arrays You can make variables hold more than one value of a specified type by setting that variable to an array. To do this, you can simply specify a size (or count) between brackets [ ] before the type of the variable, like this: array_of_ints : int; ten_strings_of_text : string; To do this using visual nodes all you have to do is select the little squares next to the variable's type: Array elements can be accessed like normal variables using the subscript operator. You can assign value to the elements or read them normally: main :: () {     array_of_ints : int;          array_of_ints = 6;     array_of_ints = array_of_ints; } Here is how it works in the visual nodes: You can query the count (or size) of the array using the .count operator, like this: #import "Print" main :: () {     array_of_ints : int;          if array_of_ints.count == 6 print("True"); } Here is the .count operator in the Visual panel: If you create an array of 5 ints, for example, you may access all those five ints inside the arrays bounds by using the subscript operator and index the array from the position 0 through 4. Note that first array index is always zero, therefore, arrays have zero-based indexes. For example, this code sets all the five elements in the array to a random number. #import "Random" main :: () {     arr: int;     arr = random_int_in_range(-10, 10);     arr = random_int_in_range(-10, 10);     arr = random_int_in_range(-10, 10);     arr = random_int_in_range(-10, 10);     arr = random_int_in_range(-10, 10); } If you try to access an array element out of the array bounds you risk crashing the program. This is explained in more detail here. You can see how arrays can be used in real programs in this section of the programming exercises. Arrays: Out Of Bounds Because an array is just a variable, its size is known and specified at compile time. This means it's a specified piece of memory and if you try accessing memory out of its bounds you are accessing other pieces of memory. For instance, if you create an array of 10 ints, and try to access index 10 (note that the last valid index is 9 because its indexes go from 0 to 9) you risk accessing memory with invalid data or unaccessible memory and crashing the program. Make sure your program only accesses arrays using valid indexes. Global Variables As discussed in the scopes section, you can create variables inside local scopes (and only access it inside that scope and its sub-scopes) or in the global scope. If you create a variable in the global scope (or a global variable), you can access it anywhere in the program, independent of the location of declaration. For example: global_variable := 5; main :: () {     local_varaible := global_variable; } This can be very useful and convenient so you don't need to pass values as parameters if a lot of procedures need to access that value. However because identifier names can collide, you have to be wary of creating too many global variables and polluting the global scope (preventing local variables to be created with those names). Default Initialization As hinted before in this page, when you create a variable and don't assign any value to it, it automatically assigned to a default value: zero in the case of ints and floats, false in bools and empty text in strings. #import "Print" main :: () {     int_var : int;     if int_var == 0  print("true");     float_var : float;     if float_var == 0.0  print("true");     bool_var : bool;     if bool_var == false  print("true");     string_var : int;     if string_var == ""  print("true"); }