Variable

From Mario Fan Games Galaxy Wiki
(Redirected from Variables)

A variable is a memory address that holds some sort of value (like ones used in algebra), and is usually named. The exact syntax for describing a variable differs by language, with higher-level languages such as scripting languages having a less verbose syntax.

Generally, most languages allow at least values and strings to be stored in a variable, but others allow more datatypes such as functions to be stored in this manner. Scripting languages are often dynamically-typed, meaning they allow a variable to change its contents to different types, e.g. an integer to a string. In contrast, statically-typed languages like C++ have variables assigned to a specific type, and incompatible values must be cast (if possible) into a compatible type before they can be assigned to that variable.

Not all variables can be accessed at any time or from any location; these limitations are defined by the variable's scope. The following is a list of various types and scopes of a variable:

Scope

  • Global: A global variable's scope is usually unlimited; global variables can usually be accessed from any function at any time. It is considered bad programming practice to overuse global variables as there is a higher risk of name collisions.
  • Local: Local variables can only be accessed from the block in which they are defined; some languages may or may not expand the scope to cover nested blocks or similar constructs. Local variables only last for a limited time; they and their contents are destroyed once the block in which they reside has run.

Types

  • Constant: A constant variable is "locked" after it is defined, and its contents can't be changed.
  • Pointer: Pointers store the memory address of another variable, allowing access to that variable's contents, or the memory contents of any other address. Arrays use pointers extensively to access their stored variables.