Global Variables

This topic is meant to document global variables in Pile

Overview

In Pile, a global variable is a value bound to a name at runtime. When used, its stored value is pushed on the stack.

A global variable can:

  • Not be destroyed. Its name is always defined during the execution of the entire program.
  • Be overwritten.

Defining a Global Variable

To define a variable in Pile, use the let statement.

Use the let keyword followed by the name of the variable.

NOTE: let statement needs a value on top of the stack to bind. If there's no value, you'll see an Unbound Variable error.

Syntax:

let <NAME>

Code examples

Here's an example demonstrating a simple variable containing a string:

"Message" let message
message println
# Output: Message

Here's another example showing how variables can be used in procedures to work as named arguments:

proc square
let number
number dup *
end

8 square println
# Output: 64

In this example, the variable number is still valid, but with use of Local Variables, this can be avoided.