Tutorial 10: Variables Extended

In this tutorial we'll be covering some of the more advanced properties of variables, what words like "Global" and "Local" mean and a concept called scope.

So far we know that we can use a variable to store a piece of information. This can be anything at all: we can store a number in a variable, we can store a piece of text (a string), we can store the result of a function, we can even store multiple pieces of information in a type of variable called an array.

This is all wonderful information. However, we're missing something a little important for more advanced programs.

Every variable in a program has something called scope. There are two levels of scope, global and local. The scope of a variable determines which parts of a program can access it.

Global variables can be accessed from anywhere in the program. Local variables can only be accessed from areas of the program with the same scope.

Confused?

Let's take a look at an example:

  1. a = 10
  2.
  3. loop
  4.     clear()
  5.     print( a + modifier( a ) )
  6.     update()
  7. repeat
  8.
  9. function modifier( number )
 10.     number += a
 11. return number

Here we have a very simple program which adds two numbers together.

The variable at the top of the program, a is global. Once we've defined it at the top of our program, we can use it anywhere we like. In the example we have demonstrated this by using the a variable both in our main loop and in our user-defined function called modifier().

The second variable in our program, number is local to the modifier() function. Because it was defined in our custom function, we can only access it within that function.

Data Types

When storing a piece of data in a variable in FUZE4 Nintendo Switch, most of the time you do not need to specify what type of data that will be. For example, let's say we want to store a whole number:

  1. a = 10

There we go! Nothing more required. Let's say we want to store a string instead:

  1. a = "Dave"

Done! What about storing an array inside our variable?

  1. a = [ 0, 1, 2, 3 ]

Simple as that! How about storing a structure?

  1. a = [ .name = "Dave", .age = 27, .interests = "Music" ]

"What about a vector?!" We hear you scream. If you don't know what a vector is, fear not! We'll be covering that in the upcoming tutorials.

  1. a = { 0, 0, 0, 0 }

Simple as can be. We do not need to define what type of data will be in a variable to store it.

However, in FUZE4 Nintendo Switch, there is one particular time where we must define the type of data.

There is a different way to create a structure, by creating something called a "structure type":

  1. struct person_type
  2.     string name
  3.     int    age
  4.     array  interests[3]    
  5. endstruct
  6.
  7. person_type people[10]

The example above defines a structure type called person_type, which describes three properties.

Line 7 creates an array of structures called people. It has 10 elements, and each element now contains a structure with three properties, a string variable called name, an int variable called age and an array variable called interests with 3 elements.

When defining a structure using a structure type like the example above, we must define what type of data each property will be. If we do not do this, we will get an error.

This way of creating structures can be very useful for larger programs, where you might need to use a certain type of structure in multiple places in your code.

Functions and Keywords Used in this Tutorial

array, clear(), endStruct, function, int, loop, print(), repeat, return, string, struct, update()