Understanding Scope in VEXcode Pro V5

The VEX Visual Studio Code Extension has replaced VEXcode Pro V5, which is now end-of-life.

VEXcode Blocks and VEXcode Text remain actively developed and supported for all VEX platforms.

Well, you need to know about Scope Rule - Understanding the ideas of “scope” with {... }

Local Variables

Observe the output generated from the sample code. It should be clear about the main difference. These are all “local” variables, within its {...} scope.

int main( ) {
int X = 10, Y = 15;
{
  int X = 20;
   {
     int X = 30, Y = 35;
     brain.Screen.printAt(4,30,  "3rd one: %d, %d ", X, Y); 
   }
  	 brain.Screen.printAt(4, 60, "2nd one: %d, %d", X, Y); 
	}
	brain.Screen.printAt(4, 90, "1st one: %d, %d", X, Y);	
	return 0;
}

Output:

3rd one: 30, 35 
2nd one: 20, 15 
1st one: 10, 15

The example above is used for quick and easy clarification. Don't use identical vertical variables within nested scopes.

How about putting the variables outside the “main( )” block?

Global Variables

Putting variables outside the main( ) block, but not within any other functions, will make variables globally usable by all functions in the project. These are referred to as global variables.

int gValue = 90000;         // this is global to all other scope
  
int main() {

  Brain.Screen.setFont(fontType::mono30);
  int X = 10;
  {
      int X = 20, gValue = 8000;
      {
         int X = 30, gValue= 700;
         Brain.Screen.printAt(4, 30, " 3rd one: %d ", gValue + X); 
      }
      Brain.Screen.printAt(4, 60, " 2nd one: %d ", gValue + X); 
   }
   Brain.Screen.printAt(4, 90, " 1st one: %d ", gValue + X);
}

Output:

3rd one: 730
2nd one: 8020 
1st one: 90010

The best practice is not to use the same identifier names for both global and local variables.

Attention: Do note that I mentioned: “project”, not “file”. A project can consist of more than one file. However, within a single project, you cannot have more than one main( ) function. This is outside the scope of this article. There are some additional rules like using “extern” regarding global variables. This will be further discussed in another section regarding multiple files within a single project within the VEX Library.


What is “namespace vex”?

Namespace does not necessarily fall into the discussion of “local” vs. “global” variables per se, since Namespace is not a variable. However, “namespace vex” does contain its own scope. Since it is part of the template code base that you MUST include in all your V5 program, the scope rule for namespace should be covered briefly in this article.

Each namespace is like a space where you can create a set of variables, functions pertaining to that individual “space.” You gain access to all the available variables, functions, classes, etc. defined inside this “space.”

You may look up the namespace online at https://api.vexcode.cloud/v5/html/. For quick lookup, you can also right-click on the “vex,” then click on “Go to Definition.” You will find a wealth of information of what is available from the namespace “vex.”

Code snippet illustrating programming concepts in V5 category, featuring example code structure and syntax for educational purposes.

You can access any of them by doing: vex::

e.g. Refer back to the template code:

using namespace vex;
vex::brain Brain;

In addition, any names including variables, functions, etc. declared inside this “vex” space won’t conflict with another “space” in the program.

For example, you can decide to create "myOwnNamespace," and title a variable "Brain." It's not recommended that you do something like that, but the program will allow it. That's because this is defined within "myOwnNamespace."

For more information, help, and tips, check out the many resources at VEX Professional Development Plus

Last Updated: