Variables

From Rosetta Code
Task
Variables
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Demonstrate a language's methods of:

  •   variable declaration
  •   initialization
  •   assignment
  •   datatypes
  •   scope
  •   referencing,     and
  •   other variable related facilities



11l

To declare a as an integer:

Int a

Type of variables may be inferred:

V a = 1

Multiple variables may be defined in a single line as follows:

Int p, a, d

360 Assembly

assignment, reference, referencing
*        value of F
         L      2,F                assigment      r2=f
*        reference (or address) of F
         LA     3,F                reference      r3=@f
*        referencing (or indexing) of reg3       (r3->f)
         L      4,0(3)             referencing    r4=%r3=%@f=f
declarations, initialization, datatypes
*        declarations                             length
C        DS     C                  character        1
X        DS     X                  character hexa   1
B        DS     B                  character bin    1
H        DS     H                  half word        2
F        DS     F                  full word        4    
E        DS     F                  single float     4    
D        DS     D                  double float     8
L        DS     L                  extended float  16
S        DS     CL12               string          12
P        DS     PL16               packed decimal  16
Z        DS     ZL32               zoned  decimal  32
*        declarations + initialization
CI       DC     C'7'               character        1
XI       DC     X'F7'              character hexa   1
BI       DC     B'11110111'        character bin    1
HI       DC     H'7'               half word        2
FI       DC     F'7'               full word        4    
EI       DC     F'7.8E3'           single float     4    
DI       DC     D'7.8E3'           double float     8
LI       DC     L'7.8E3'           extended float  16
SI       DC     CL12'789'          string          12
PI       DC     PL16'7'            packed decimal  16
ZI       DC     ZL32'7'            zoned decimal   32
scope

In BAL (Basic Assembler Language), variables are global and there is no scope.

6502 Assembly

Declaration

Declaration is not actually needed in 6502 Assembly. Any portion of RAM can be read or written at any time. However, it is very helpful to use descriptive labels of memory locations. Different assemblers handle this differently, with directives such as "Equals" equ, "Enumerate" .enum, "Reserve Storage Space" .rs, or "Define Storage Bytes:.dsb. These directives allow you to name a memory address to something you can more easily remember than an arbitrary number. One important caveat to note is that the equ directives do not take up space in your program. This is easier to illustrate with an example than explain in words:

    org $1200
SoundRam equ $1200             ;some assemblers require equ directives to not be indented.
SoundChannel_One equ $1200
SoundChannel_Two equ $1201
SoundChannel_Three equ $1202
     LDA #$FF ;this still gets assembled starting at $1200, since equ directives don't take up space!


Each example below is equivalent, but which one you use depends on the assembler.

The equ method:

joystick equ $00 ;this variable is located at zero page memory address $00
Player_Xpos equ $01 ;this variable is located at $01
Player_Ypos equ $02
pointer equ $03     ;intended to take up 2 bytes
sound_on equ $05

The enum method:

enum $0000
;these will be defined starting at $0000 and increasing in the order listed, incremented by the amount after DSB.
joystick dsb 1 ;this takes up 1 byte
Player_Xpos dsb 1
Player_Ypos dsb 1
pointer  dsb 2 ;this takes up 2 bytes
sound_on dsb 1
ende           ;end enumeration

The rs method:

.rsset $0000
joystick    .rs 1
Player_Xpos .rs 1
Player_Ypos .rs 1
pointer     .rs 2
sound_on    .rs 1
;no closer needed for this method

Initialization

This mostly depends on the hardware you are programming for. If the code runs in RAM, you can initialize an entire block of RAM to the value of choice with ds <count>,<value> (replace count and value with numbers of your choice, no arrow brackets.) On systems like the NES which have dedicated RAM areas on the CPU and the whole program is in ROM, you're best off setting all RAM to zero on startup and initializing the ram to the desired values at runtime.

Assignment

Assignment is done by storing values into memory. Variables larger than 8 bits will need to be loaded in pieces.

LDA #$05
STA Player1_Lives ;equivalent C code: Player1_Lives = 5;

LDA #$05
STA pointer+1
LDA #$40
STA pointer
;loads the variable pointer with the value $0540

Datatypes

The 6502 can only handle one data type natively: 8 bit. Anything larger will need to be represented by multiple memory locations. Since the CPU is little-endian the low byte should be "first," i.e. if you have a variable declared such as pointer dsb 2 then the low byte must be stored in pointer and the high byte in pointer+1. (You do not declare pointer+1 separately; the assembler will take pointer+1 to mean the memory address directly after pointer. The 6502 uses consecutive zero-page memory locations as a means of indirect addressing.)

Scope

The 6502 has no concept of scope; every variable is global. Assemblers can enforce scope rules to allow the reuse of labels you would want to use frequently, like "skip," "continue," "done," etc., but normally each label can't be defined more than once in the same program. You can trick the assembler into (effectively although not technically) letting you re-use the same label with bank switching but for the most part each label must be unique.

Referencing

Labels are always defined at compile time. The assembler converts each label into the value or address it represents as part of the assembling process. The CPU doesn't know the labels even exist; labels are simply a convenience for the programmer. Once defined, a label can be used in place of an address or value.

NametableBase equ $20 ;label referring to a constant
VRAM_ADDR     equ $2006 ;label referring to a memory address
VRAM_DATA     equ $2007 ;label referring to a memory address

LDA #NametableBase   ;without a # this is interpreted as a memory address like any other number would be.
STA VRAM_ADDR
LDA #$00
STA VRAM_ADDR

LDA #$03
STA VRAM_DATA

68000 Assembly

Typically a variable is just a named memory location that exists in RAM. For ROM cartridge software, equ directives are used to assign names to memory locations. For programs that run from disk, which are loaded into RAM and executed from there, the DC directives can also be used, and then the programmer places a label in front of those directives.

MyVar:
DC.L 0      ;reserves 4 bytes of storage. The label MyVar represents the address of the four 0 bytes shown here.

MyOtherVar:
DC.L $C0   ;reserves 8 bytes of storage. The first four bytes are initialized to 00 00 00 C0 and the second four to $00 $00 $01 $F4.
DC.L 500   ;MyOtherVar points to the first four bytes, you'll need pointer arithmetic to get to the second four.

8086 Assembly

A variable is just a value in some memory location that can be read from and written to. Declaring a variable in an assembler can be done with labels. If your code is executed from RAM, as is the case with most home computers, variables can be initialized prior to runtime.

.data
MyVar word 0FFFFh  ;the leading zero is just to help the assembler tell that this is a number, it's not actually part of the variable.

.code

mov ax, word ptr [ds:MyVar]

Programs written for ROM cartridge-based systems like the WonderSwan can't use the method above to initialize variables at compile time; however the programmer can equate a memory location with a label that describes what will be stored there. Unfortunately this means that you can't be sure what the system's RAM will contain at startup, so you'll usually have to wipe it clean before the user is allowed to play.

Player1_Lives equ 0100h
mov al,3
mov byte ptr [Player1_Lives],al   ;give the player 3 lives to start the game with

Scope is a little more complicated on 8086 Assembly than most other assembly languages. The segmented memory model means that a segment register must point to the segment where a variable is located before it can be used; however in practice this takes little effort on the programmer's part, regardless of where they are in the program. There is nothing stopping you from changing the values stored in segment registers, however if you have multiple variables in multiple different segments you'll end up juggling which segments are accessible at a given point, which is not a good place to be. Fortunately, you don't need to actually remember which segment a labeled variable is stored in, as the assembler can work that out for you:

 mov ax, seg MyData  ;the assembler replaces this with the segment MyData is located in prior to assembling the program.
mov ds,ax                      ;load this segment into the data segment register.

Some segments, such as those for video memory, can't be reliably looked up with seg. Thankfully, they're nearly always constant values, so you can just use an equ directive to label them.

8th

There is only one type of variable in 8th, and it is simply a single-item container which can contain any of the known data-types:

\ declare a variable which is initialized to the number '0'
var x

\ declare a variable which is initialized to a string "cat"
"cat" var, y

\ Get the value in x, add 20 and store it:
x @ 20 n:+ x !

\ Change the cat to a dog:
"dog" y !

AArch64 Assembly

Works with: as version Raspberry Pi 3B version Buster 64 bits
/* ARM assembly AARCH64 Raspberry PI 3B */
/*  program variable64.s   */
 
/*******************************************/
/* Constantes file                         */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
/*********************************/
/* Initialized data              */
/*********************************/
.data
szString:         .asciz "String définition"
sArea1:           .fill 11, 1, ' '            // 11 spaces
     // or
sArea2:           .space 11,' '               // 11 spaces
 
cCharac:          .byte '\n'                  // character
cByte1:           .byte 0b10101               // 1 byte binary value
 
hHalfWord1:       .hword   0xFF               // 2 bytes value hexa
.align 4
iInteger1:        .int 123456                 // 4 bytes value decimal
iInteger3:        .short 0500                 // 4 bytes value octal
iInteger5:        .int 0x4000                 // 4 bytes value hexa

iInteger7:        .word 0x4000                //   4 bytes value hexa
iInteger6:        .int  04000                 //   4 bytes value octal
 
TabInteger4:      .int  5,4,3,2               // Area of 4 integers = 4 * 4 = 16 bytes
 
dDoubleInt1:      .quad  0xFFFFFFFFFFFFFFFF   //  8 bytes value hexa
 
dfFLOAT1:         .double 0f-31415926535897932384626433832795028841971.693993751E-40 //  Float 8 bytes
sfFLOAT2:         .float  0f-31415926535897932384626433832795028841971.693993751E-40 //  Float 4 bytes (or use .single)
 
/*********************************/
/* UnInitialized data            */
/*********************************/
.bss  
sBuffer:         .skip  500                      // 500 bytes values zero
iInteger2:       .skip 4                         // 4 bytes value zero
dDoubleint2:     .skip 8                         // 8 bytes value zero
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                                            // entry of program 
    ldr x0,qAdriInteger2                         // load variable address
    mov x1,#100                                    
    str x1,[x0]                                  // init variable iInteger2
 
100:                                             // standard end of the program 
    mov x0, 0                                    // return code
    mov x8, EXIT                                 // request to exit program
    svc 0                                        // perform the system call
 
qAdriInteger2:             .quad iInteger2       // variable address iInteger2

Ada

Name: declare   -- a local declaration block has an optional name
   A : constant Integer := 42;  -- Create a constant
   X : String := "Hello"; -- Create and initialize a local variable
   Y : Integer;           -- Create an uninitialized variable
   Z : Integer renames Y: -- Rename Y (creates a view)
   function F (X: Integer) return Integer is
     -- Inside, all declarations outside are visible when not hidden: X, Y, Z are global with respect to F.
     X: Integer := Z;  -- hides the outer X which however can be referred to by Name.X
   begin
     ...
   end F;  -- locally declared variables stop to exist here
begin
   Y := 1; -- Assign variable
   declare
     X: Float := -42.0E-10;  -- hides the outer X (can be referred to Name.X like in F)
   begin
     ...
   end;
end Name; -- End of the scope

ALGOL 68

Local variables are generally called local variables in ALGOL 68. Variables must be declared before use. In traditional ALGOL 68, variables must be declared before any labels: in a compound-clause. The declaration of a variable, without assigning a value takes the form: <typename> <variablename>;

int j;

Some common types are: char, string, short int, int, long int, real, long real, bits and bytes .

Multiple variables may be defined in a single statement as follows:

LONG REAL double1, double2, double3;

It is possible to initialize variables with expressions having known values when they are defined. The syntax follows the form <typename> <variablename> := <initializing expression>;

SHORT INT b1 := 2500;
LONG INT elwood = 3*bsize, jake = bsize -2;

The strings in ALGOL 68 are flex arrays of char. To declare initial space for a string of exactly to 20 characters, the following declaration is used.

FLEX[20]CHAR mystring;

All arrays are structure that include both the lower lwb and upper upb of the array. Hence strings in ALGOL 68 may safely contain null characters and can be reassigned with longer or shorter strings.

To declare an initialized string that won't be changed the following declaration may be used:

[]CHAR mytext = "The ALGOL 68 Language";

There are more rules regarding arrays, variables containing pointers, dynamic allocation, and initialization that are too extensive to cover here.

ALGOL W

Algol W is block structured, following the rules established by Algol 60. There are variables of the following basic types: integer, real, long real, complex, long complex, bits, logical, string.

Logical variables (usually called boolean in other languages) hold true/false values. Bits variables hold bit strings. Strings are fixed length, between 1 and 256 characters long. The length is part of the type. case is not significant in variable names.

Declarations must appear at the beginning of the block they are contained in, before the first executable statement. Declaration is separate from initialisation - there is no separate initialisation syntax (except for fields of a record - see below), normal assignments are used:

% declare some variables %
integer a1, a2; real b; long real c; complex d; long complex f;
logical g; bits h; string(32) j;

% assign "initial values" %
f := d := c := b := a2 := a1 := 0; % multiple assignment %
g := false; h := #a0; j := "Hello, World!";

Records can be declared, composed of fields of the basic types and references to records. E.g.:

record R1 ( integer length; string(256) text );
reference(R1) ref1, ref2;

In the above, R1 is a structure containing an integer and a string. Ref1 and ref2 are variables that will refer to instances of the R1 structure. References can be declared that can refer to a number of different record structures. The allowable references must be specified in the declaration E.g.:

record person( string(32) name; integer age );
record date( integer day, month, year );
reference(person, date) ref3;

In the above, ref3 can hold references to either a person or a date. Variables that are references to the basic types are not allowed. E.g.:

reference(integer) refInt; % an illegal declaration %

The following could be used instead:

record INT_VALUE ( integer val );
reference(INT_VALUE) refInt;

Fields are referred to via a function-like notation, e.g.:

% using the person record defined above...%
    reference (person) someone;
    someone := person           % create a new person structure with uninitialised fields %
    name(someone) := "Fred";    % initialise the fields %
    age(someone)  := 27;
    % could also initialise the fields when the record is created: %
    someone := person( "Harry", 32 );

Arrays of the basic types and references can also be declared, but records cannot contain arrays. There are no procedure variables though procedures can be passed as parameters to other procedures, as can arrays, references and the basic types. Procedures can return basic types and references.

Apex

// If not initialized at class/member level, it will be set to null
Integer x = 0;
Integer y; // y is null here
Integer p,q,r; // declare multiple variables
Integer i=1,j=2,k=3; // declare and initialize

/*
 * Similar to Integer, below variables can be initialized together separated by ','.
*/
String s = 'a string';
Decimal d = 0.0;
Double dbl = 0.0;
Blob blb = Blob.valueOf('Any String');
Boolean b = true;
AClassName cls = new AClassName();

AppleScript

Variables are untyped in AppleScript, but they must be instantiated before use.

Example:

set x to 1

Scope may be explicitly defined before instantiation using either the global or local declarations.

global x
set x to 1
local y
set y to 2

If undeclared, AppleScript will automatically set the scope based on the following rule: variables declared at the top level of any script will be (implicit) globals, variables declared anywhere else will be (implicit) locals. Scope cannot be changed after being explicitly or implicitly defined.

Where a variable has both local and global instances, it is possible to use the my modifier to access the global (top-level) instantiation.

Example:

on localx()
	set x to 0 -- implicit local
	return x
end localx

on globalx()
	set x to 0 -- implicit local
	return my x
end globalx

on run
	set x to 1 -- top-level implicit global
	return {localx(), globalx()}
end run
--> RETURNS: {0, 1}

Applescript also supports top-level entities known as properties that are global to that script.

Example:

property x : 1

Properties behave exactly as global variables except that they are persistent. Their most recent values are retained between script executions (or until the script is recompiled).

ARM Assembly

Works with: as version Raspberry Pi
/* ARM assembly Raspberry PI  */
/*  program variable.s   */
 
/************************************/
/* Constantes Définition            */
/************************************/
.equ STDOUT, 1     @ Linux output console
.equ EXIT,   1     @ Linux syscall
.equ WRITE,  4     @ Linux syscall
/*********************************/
/* Initialized data              */
/*********************************/
.data
szString:         .asciz "String définition"
sArea1:            .fill 11, 1, ' '            @ 11 spaces
     @ or
sArea2:            .space 11,' '               @ 11 spaces

cCharac:          .byte '\n'                   @ character
cByte1:            .byte 0b10101               @ 1 byte binary value

hHalfWord1:       .hword   0xFF                @ 2 bytes value hexa
.align 4
iInteger1:      .int 123456                    @  4 bytes value decimal
iInteger3:      .short 0500                    @  4 bytes value octal
iPointer1:      .int 0x4000                    @   4 bytes value hexa
     @ or
iPointer2:      .word 0x4000                   @   4 bytes value hexa
iPointer3:       .int  04000                   @   4 bytes value octal

TabInteger4:     .int  5,4,3,2                 @ Area of 4 integers = 4 * 4 = 16 bytes

iDoubleInt1:     .quad  0xFFFFFFFFFFFFFFFF     @  8 bytes

dfFLOAT1:       .double 0f-31415926535897932384626433832795028841971.693993751E-40 @  Float 8 bytes
sfFLOAT2:       .float  0f-31415926535897932384626433832795028841971.693993751E-40 @  Float 4 bytes (or use .single)

/*********************************/
/* UnInitialized data            */
/*********************************/
.bss  
sBuffer:         .skip  500                       @ 500 bytes values zero
iInteger2:       .skip 4                          @ 4 bytes value zero
/*********************************/
/*  code section                 */
/*********************************/
.text
.global main 
main:                                             @ entry of program 
    ldr r0,iAdriInteger2                          @ load variable address
    mov r1,#100                                    
    str r1,[r0]                                   @ init variable iInteger2

100:                                              @ standard end of the program 
    mov r0, #0                                    @ return code
    mov r7, #EXIT                                 @ request to exit program
    svc #0                                        @ perform the system call
 
iAdriInteger2:             .int iInteger2         @ variable address iInteger2

Arturo

num: 10
str: "hello world"

arrA: [1 2 3]
arrB: ["one" "two" "three"]
arrC: [1 "two" [3 4 5]]
arrD: ["one" true [ print "something" ]]

dict: [
	name: "john"
	surname: "doe"
	function: $[][
		print "do sth"
	]
]

inspect symbols

AutoHotkey

x = hello       ; assign verbatim as a string
z := 3 + 4      ; assign an expression 
if !y       ; uninitialized variables are assumed to be 0 or "" (blank string)
Msgbox %x%  ; variable dereferencing is done by surrounding '%' signs
fx()
{
local x   ; variable default scope in a function is local anyways
global y  ; 
static z=4  ; initialized once, then value is remembered between function calls
}

AWK

Dynamic variables

BEGIN { 
# Variables are dynamically typecast, and do not need declaration prior to use:
  fruit = "banana"    # create a variable, and fill it with a string 
  a = 1               # create a variable, and fill it with a numeric value
  a = "apple"         # re-use the above variable for a string
  print a, fruit
  
# Multiple assignments are possible from within a single statement: 
  x = y = z = 3
  print "x,y,z:", x,y,z
  
# "dynamically typecast" means the content of a variable is used 
# as needed by the current operation, e.g. for a calculation:
  a = "1"
  b = "2banana"
  c = "3*4"

  print "a,b,c=",a,b,c,  "c+0=", c+0, 0+c
  print "a+b=", a+b, "b+c=", b+c
}
Output:
apple banana
x,y,z: 3 3 3
a,b,c= 1 2banana 3*4 c+0= 3 3
a+b= 3 b+c= 5

Numeric values are always real, there is no separate datatype for integer.

awk also has Arrays.

Assignment on commandline

It is possible to assign value to variables from the commandline:

# usage:  awk -v x=9  -f test.awk
BEGIN {
  y = 3
  z = x+y
  print "x,y,z:", x,y,z
  printf( "x=%d,y=%d,z=%d:", x,y,z )
}
Output:

with "-v x=9"

x,y,z: 9 3 12
x=9, y=3, z=12
Output:

without "-v" (x stays empty, which counts as zero in calculations)

x,y,z:  3 3   
x=0, y=3, z=3 

Global scope

Variables have global scope, and there is no way to make a variable local to a block.

However, function arguments are local, so it is possible to make a variable local to a function
by listing the variable as an additional dummy function argument after the required arguments:

function foo(s,  k) {
  # s is an argument passed from caller
  # k is a dummy not passed by caller, but because it is 
  # in the argument list, it will have a scope local to the function
  k = length(s)
  print "'" s "' contains", k, "characters"
}
BEGIN {
  k = 42
  s = "Test"
  foo("Demo")
  print "k is still", k
  foo(s,k)
  print "k still is", k
}
Output:
'Demo' contains 4 characters 
k is still 42
'Test' contains 4 characters 
k still is 42

Builtin variables

There are some special variables:
When reading a line of input, the contents of that line are automatically split into a number of variables:

  • $0 is the whole input-line
  • $1 is the first field
  • $2 is the 2nd field, etc.
  • NF is the number of fields
  • $NF is the contents of the last field.
# Feeding standard-input with echo:
echo -e "2 apples 0.44$ \n 3 banana 0.33$" | awk '{p=$1*$NF; sum+=p; print $2,":",p; }; END{print "Sum=",sum}'

With more data, this would look like

 awk '{p=$1*$NF; sum+=p; print $2,":",p; }; END{print "Sum=",sum}' inputfile

or more typically:

 awk  -f report.awk  inputfile
Output:
apples : 0.88
banana : 0.99
Sum= 1.87

Axe

In Axe, variables are global because they are (mostly) static references to memory locations. This means there is also no scope.

1→A

The letters A-Z and theta are all general-purpose variables. They are located at the end of the L₁ memory region by default, but they can be reallocated to any region of free memory:

#Realloc(L₂)

The variables r₁ through r₆ are used for function arguments. They are automatically set as the arguments passed to the function. However, they behave otherwise just like normal variables.

Other variables include pointers such as Str0-Str9, GDB0-GDB9, and Pic0-Pic9. These are effectively constant and are assigned at compile time to point to static data.

BASIC

In BASIC, variables are global and there is no scope. However, it is an error to reference a variable before it has been assigned.

10 LET A=1.3
20 LET B%=1.3:       REM The sigil indicates an integer, so this will be rounded down
30 LET C$="0121":    REM The sigil indicates a string data type. the leading zero is not truncated
40 DIM D(10):        REM Create an array of 10 digits
50 DIM E$(5.10):     REM Create an array of 5 strings, with a maximum length of 10 characters
60 LET D(1)=1.3:     REM Assign the first element of d
70 LET E$(3)="ROSE": REM Assign a value to the third string
80 PRINT D(3):       REM Unassigned array elements have a default value of zero
90 PRINT E$(3):      REM Ten spaces because string arrays are not dynamic
100 PRINT E$(3);"TTA CODE": REM There will be spaces between rose and etta
110 DIM F%(10):  REM Integers use less space than floating point values
120 PRINT G:     REM This is an error because f has not been defined
130 PRINT D(0):  REM This is an error because elements are numbered from one
140 LET D(11)=6: REM This is an error because d only has 10 elements
150 PRINT F%:    REM This is an error because we have not provided an element number
160 END

Applesoft BASIC

In Applesoft BASIC, variables are global and there is no scope. And, it is not an error to reference a variable before it has been assigned. The LET keyword is optional. Almost all math is done using floating point numbers by default. Using floating point variables is almost always faster than using integer variables which require extra conversion between floating point and integer. Integers use less space than floating point values. Applesoft BASIC array indexes start at zero.

 10  A = 1.7: REM LET IS NOT REQUIRED
 20   LET B% = 1.7: REM THE PERCENT SIGN INDICATES AN INTEGER; THIS GETS TRUNCATED DOWN
 30   LET C$ = "0121": REM THE DOLLAR SIGN INDICATES A STRING DATA TYPE. THE LEADING ZERO IS NOT TRUNCATED
 40   DIM D(20): REM CREATE AN ARRAY OF 21 FLOATING POINT NUMBERS
 50   DIM E$(5,10): REM CREATE A TWO DIMENSIONAL ARRAY OF 66 STRINGS
 60   LET D(1) = 1.3: REM ASSIGN THE SECOND ELEMENT OF D
 70  Y$(3) = "ROSE": REM ASSIGN A VALUE TO THE FOURTH STRING
 80   PRINT X: REM UNASSIGNED FLOATING POINT AND INTEGER VARIABLES HAVE A DEFAULT VALUE OF ZERO
 90   PRINT Y$(2): REM UNASSIGNED STRING VARIABLES ARE EMPTY
 100  PRINT Y$(3);"TTA CODE": REM THERE WON'T BE SPACES BETWEEN ROSE AND ETTA
 110 F%(10) = 0: REM IF ARRAYS ARE NOT DECLARED THEY HAVE 11 ELEMENTS BY DEFAULT; IE. DIM F%(10)
 120  PRINT G: REM THIS PRINTS 0 AND IS NOT AN ERROR EVEN THOUGH G HAS NOT BEEN DEFINED
 130  PRINT D(0): REM THIS IS NOT AN ERROR BECAUSE ELEMENTS ARE NUMBERED FROM ZERO.
 140  PRINT F%: REM THIS PRINTS 0 BECAUSE F% IS A DIFFERENT VARIABLE THAN THE ARRAY F%(10)
 150  LET D(21) = 6: REM THIS IS AN ERROR BECAUSE D ONLY HAS 21 ELEMENTS INDEXED FROM 0 TO 20.

Commodore BASIC

In Commodore BASIC, variables are global and there is no scope, and as with other BASIC dialects, it is not an error to reference a variable before it has been assigned.

The LET keyword is optional. Almost all math is done using floating point numbers by default. Using floating point variables is almost always faster than using integer variables which require extra conversion between floating point and integer. Integers use less space than floating point values.

Naming and Data Types

A variable can be uniquely identified with a maximum of two (2) alpha-numeric characters, and the first must be a letter. The type of variable is identified with either a % or $ or no symbol at all. Certain reserved words that are two characters long cannot be used as variables (e.g. TO, ON, etc.)

Symbol Variable Type
(none) Floating point number
% Integer number
$ String

There are functions available to convert between number and string types.

Comparison of Variables

All variables, including strings, can be compared using the comparitive operators <,>, and =. Note: String comparison follows character order in Commodore PETSCII, that is, A is less than in uppercase/graphics mode, while A is greater than a in mixed case mode. Conventional ASCII would have a greater than A.

Arrays

Arrays can be used for any of the above types. In other dialects of BASIC, it may be necessary to initialize an array with the DIM command before any use, however in Commodore BASIC, one and two dimensional arrays of up to 11 elements (index 0 through 10) in one or both dimensions may be used without any initial DIMensioning. Any array larger than 11 elements in either dimension, or more than two dimensions of any size, must be initialized with DIM prior to use.

In theory, arrays can be of any size, however BASIC memory limits must be observed. For example, a single dimension string array of 256 elements (0 through 255) would theoretically occupy ALL of the Commodore 64's 64 kilobytes of RAM... 255 elements each holding 255 bytes of string data. However, only a little under 40 kilobytes is available in all of the default BASIC RAM space, which must be shared for program storage as well as runtime variable storage.

With each of the three data types, and including the possibility of arrays, a, a%, a$, a(0), a%(0), and a$(0) represent at least six (6) unique variables, plus any additional elements which might be part of the array definitions.

Demonstration Program

1 rem rosetta code
5 rem commodore basic variable demonstration
10 print chr$(147);chr$(14);:ti$="000000":rem see lines 420-460
15 rem numeric variables default to 0; strings default to empty
20 print a:print b%:print c$:print
25 :
30 rem no symbol after variable defaults to float.
35 let a=1.7
40 rem "let" is not required and rarely used.
45 b=2.42
50 print a:print b
55 rem % means integer type; digits after decimal are truncated
60 b%=1.7
65 print b%
70 rem $ means string type
75 c$="Commodore"
80 print c$:print
85 :
90 rem each type is unique, even when name is "same"
95 a=5.0
100 a%=9
105 a$="twenty-five"
110 print a:print a%:print a$:print
115 :
120 rem names unique only to two characters; extra ignored
125 li=10:lives=8:lights=64
130 print li:print lives:print lights:print
135 rem second character can be alphanumeric, but is not array
140 s1=100 : s2=200 : s3=300
145 print s1:print s2:print s3:print
150 gosub 5000
155 :
160 rem strings preserve all literal characters
165 rem numerics drop leading zeros and trailing zeros after decimal
170 n$="01276":print n$:rem 01276
175 o%=01276:print n%: rem 1276
180 p=4.900:print p: print: rem 4.9
185 :
190 rem string-numeric conversion functions
195 c$="05034"
200 c%=val(c$) : rem converts to the numeric value of 5034 (first zero dropped)
205 d=123.45600 : rem define a float
210 d$=str$(d) : rem converts above into a string
215 print c$:print c%:print d:print d$:print
218 :
220 rem strings can be ordered/compared > or < like numbers
225 input "Enter a string";x$:print
230 input "Enter another string";y$:print
235 if x$>y$ then print x$;" comes after ";y$
240 if x$<y$ then print x$;" comes before ";y$
245 if x$=y$ then print "You entered the same string twice!"
250 gosub 5000
255 :
260 rem numbers have a leading character for pos/neg sign
265 rem " " means positive
270 a=-52:b=124
275 print a:print b:print
280 :
285 rem variable operations
290 e$="endothermic":print e$
295 print left$(e$,3) : rem "end"
300 print right$(e$,3) : rem "mic"
305 print mid$(e$,4,5) : rem "other"
310 print
315 a=5:b=20:c=a+b:print a;"+";b;"=";c : rem addition
320 q=90:r=60:s=q-r:print s : rem subtraction
325 x=3:y=4:z=x*y:print x;"*";y;"=";z : rem 12 multiplication
330 l=12:m=16:n=l/m:print l;"/";m;"=";n :rem division
335 rem string concatenation
340 print
345 f$="John":l$="Jones":n$=l$+", "+f$:print f$:print l$:print n$
350 gosub 5000
355 :
360 rem arrays can be single or multidimensional
365 rem array index starts at 0
370 rem single dimenstion arrays of 11 elements or less 
375 rem do not need to be DIMensioned
380 a$(0)="first":a$(1)="second":a$(3)="third":rem we skipped index 2
385 for i=0 to 3:print a$(i):next
390 gosub 5000
395 dim b(1,20) : rem 42 elements
400 for i=0 to 1:for j=0 to 20:b(i,j)=(i+1)*j:next j,i
405 for i=0 to 1:print chr$(19):for j=0 to 20:print tab(i*6);b(i,j):next j,i
410 gosub 5000
415 :
420 rem special variables - ti and st are technically functions,
425 rem but ti$ can be assigned a value similar to a string variable
430 t$=left$(ti$,2)+":"+mid$(ti$,3,2)+":"+right$(ti$,2)
435 print "Ticks since program started:":print ti
440 print "Elapsed time since program started:":print t$
445 print "I/O Status:";st : rem i/o status
450 print:print "Enter new time (HHMMSS): ";:gosub 5200
455 ti$=t$:gosub 5500
460 print
465 :
470 rem all variables can be cleared with the "clr" statement
475 rem however, this also clears the return address stack for subroutines
480 rem making "return" not possible.
485 print "Before CLR:":print a:print a$:print a$(0):print b:print c
490 clr:print:print "After CLR:"
495 print a:print a$:print a$(0):print b:print c
500 print
505 end
600 :
700 rem supporting subroutines
800 :
5000 rem screen pause routine
5005 print:print "press a key to continue"
5010 get k$:if k$="" then 5010
5015 print chr$(147):return
5020 :
5200 rem custom time input routine
5205 t$="":for d=1 to 6
5210 get k$:if k$<"0" or k$>"9" then 5210
5215 t$=t$+k$:print k$;:next
5220 return
5230 :
5500 rem display live clock
5505 print chr$(147):print:print "Press a key to continue."
5510 print chr$(19);"Time: "left$(ti$,2)":"mid$(ti$,3,2)":"right$(ti$,2);
5515 get k$:if k$="" then 5510
5520 print chr$(147);:return

Modifications for Enhanced TIME$

Some models allow TI$ to be a seven digit number able to track 1/10 seconds as the final digit. Use the following modifications for those models (e.g. CBM-II):

10 print chr$(147);chr$(14);:ti$="0000000":rem see lines 420-460

430 t$=left$(ti$,2)+":"+mid$(ti$,3,2)+":"+mid$(ti$,5,2)+"."+right$(ti$,1)

5205 t$="":for d=1 to 7

5500 rem display live clock
5505 print chr$(147):print:print "Press a key to continue."
5510 t$=left$(ti$,2)+":"+mid$(ti$,3,2)+":"+mid$(ti$,5,2)+"."+right$(ti$,1)
5515 print chr$(19);"Time: "t$
5520 get k$:if k$="" then 5510
5525 print chr$(147);:return

Tiny BASIC

REM Tiny Basic has exactly 26 variables. 
REM They start off initialised to zero.
    PRINT A

REM The only data type is sixteen-bit signed integer.
REM They are assigned using the LET statement.
REM Their scope is the whole program. 
    LET B = -12345
REM The integer arithmetic operations of  +  -  *  and / can be used
REM and so can the unary negative and positive operators -  +
    LET C = 1 + B - B/5
    LET A = -B
    PRINT "B is ", B
    PRINT "C is ", C
    GOSUB 10

REM The comparison operators =  <  >  <=  >=  <> are available,
REM but their results are not expressions and can only be used in an
REM if statement.
    LET D = 3
    IF D <> 7 THEN LET D = 7
    GOTO D-2
    PRINT "Skip this"
 5  PRINT "Gotos and gosubs can be computed. Beware of moving spaghetti."   
    END
10  PRINT "B is now ", B
    RETURN

REM Tiny Basic does not support arrays or pointers. Strings can
REM be used, but only as string constants within a PRINT statement.

Batch File

Batch file variables are not limited to data types and they do not need to be initialized before use.

@echo off

::setting variables in defferent ways
set myInt1=5
set myString1=Rosetta Code
set "myInt2=5"
set "myString2=Rosetta Code"

::Arithmetic
set /a myInt1=%myInt1%+1
set /a myInt2+=1
set /a myInt3=myInt2+   5

set myInt
set myString
pause>nul

BBC BASIC

      REM BBC BASIC (for Windows) has the following scalar variable types;
      REM the type is explicitly indicated by means of a suffix character.
      REM Variable names must start with A-Z, a-z, _ or `, and may contain
      REM any of those characters plus 0-9 and @; they are case-sensitive.
      
      A& = 123      : REM Unsigned 8-bit byte (0 to 255)
      A% = 12345678 : REM Signed 32-bit integer (-2147483648 to +2147483647)
      A  = 123.45E6 : REM Variant 40-bit float or 32-bit integer (no suffix)
      A# = 123.45E6 : REM Variant 64-bit double or 32-bit integer
      A$ = "Abcdef" : REM String (0 to 65535 bytes)
      
      REM Scalar variables do not need to be declared but must be initialised
      REM before being read, otherwise a 'No such variable' error is reported
      REM The static integer variables A% to Z% are permanently defined.
      
      REM BBC BASIC also has indirection operators which allow variable-like
      REM entities to be created in memory:
      
      DIM addr 7    : REM Allocate 8 bytes of heap
      ?addr = 123   : REM Unsigned 8-bit byte (0 to 255)
      !addr = 12345 : REM Signed 32-bit integer (-2147483648 to +2147483647)
      |addr = 12.34 : REM Variant 40-bit or 64-bit float or 32-bit integer
      $addr = "Abc" : REM String terminated by CR (0 to 65535 bytes)
      $$addr = "Abc": REM String terminated by NUL (0 to 65535 bytes)
      
      REM The integer indirection operators may be used in a dyadic form:
      offset = 4
      addr?offset = 12345678 : REM Unsigned 8-bit byte at addr+offset
      addr!offset = 12345678 : REM Signed 32-bit integer at addr+offset
      
      REM All variables in BBC BASIC have global scope unless they are used
      REM as a formal parameter of a function or procedure, or are declared
      REM as LOCAL or PRIVATE.  This is different from most other BASICs.

Boo

Variable Declaration Boo is a statically typed language. Types can either be explicitly declared or inferred after being intitialized


x as int // declares an integer


y = 23 // declares an integer and assigns the value 23


foo as string // string declaration


foo = "Boo!" // string assignment


pi as double // double floating point declaration


stuff = 32.2322 // double assignment



Boo Variable Types

sbyte: -128 to 127

short: -32768 to 32767

int: -2147483648 to 2147483647

long: -9223372036854775808 to 9223372036854775807

byte: 0 to 255

ushort:0 to 65535

uint: 0 to 4294967295

ulong: 0 to 18446744073709551615

single: Approximately ±1.5 x 10-45 - ±3.4 x 1038 with 7 significant figures

double: Approximately ±5.0 x 10-324 - ±1.7 x 10308 with 15 significant figures

decimal: Approximately ±1.0 x 10-28 - ±7.9 x 1028 with 28 significant figures

char: Any UTF-16 character

bool: True or False

BQN

BQN variables are declared and initialized using the symbol. The following code block creates a variable a with value 10.

a  10

Variables can be modified using the symbol after they are declared.

BQN uses lexical scoping, where scopes correspond roughly to the blocks they are contained in. A more detailed description can be found in the scoping documentation or specification.

Name matching is case-insensitive, and ignores underscores. While any name can store any value, each instance of a name has a syntactic role determined by BQN's spelling rules:

  • A name with a subject role if it starts with a lowercase letter.
  • A name with a function role starts with an uppercase letter.
  • A name with a 1-modifier role starts with an underscore, and doesn't end with an underscore.
  • A name with a 2-modifier role starts with an underscore, and ends with an underscore.

Bracmat

Variable declaration. Variables local to a function (i and j in the example below) can be declared just before the body of a function.

(myfunc=i j.!arg:(?i.?j)&!i+!j)

Global variables are created the first time they are assigned a value.

Initialization. Local variables are initialised to 0.

Assignment. There are two ways.

To assign unevaluated code to a variable, you normally would use the <variable>=<unevaluated expression> syntax.

To assign an evaluated expression to a variable, you use pattern matching as in <evaluated expression>:?<variable>

Datatypes. There are no datatypes. The nature of an expression is observed by pattern matching.

Scope. Local variables have dynamic scope.

Referencing. Variables are referenced using the !<variable> syntax.

Other variable related facilities. Global variables (name as well as value) can be removed from memory with the built-in tbl function. The names of built-in functions such as put and lst can be used as variable names without adverse effects on the built-in function. It is not possible to redefine built-in functions to do something different.

C

Local variables are generally called auto variables in C. Variables must be declared before use. The declaration of a variable, without assigning a value takes the form <typename> <variablename>;

int j;

Some common types are: char, short, int, long, float, double and unsigned.

Multiple variables may be defined in a single statement as follows:

double double1, double2, double3;

It is possible to initialize variables with expressions having known values when they are defined. The syntax follows the form <typename> <variablename> = <initializing expression>;

short b1 = 2500;
long elwood = 3*BSIZE, jake = BSIZE -2;

Strings in C are arrays of char terminated by a 0 or NULL character. To declare space for a string of up to 20 characters, the following declaration is used.

char mystring[21];

The extra length leaves room for the terminating 0.

To declare an initialized string that won't be changed the following declaration may be used:

const char * mytext = "The C Language";

There are more rules regarding arrays, variables containing pointers, dynamic allocation, and initialization that are too extensive to cover here.

C#

Variables in C# are very dynamic, in the form that they can be declared practically anywhere, with any scope. As in other languages, often used variables are: int, string, double etc.

They are declared with the type first, as in C:

int j;

Multiple variables may be defined in a single line as follows:

int p, a, d;

It is also possible to assign variables, either while declaring or in the program logic:

int a = 4;
int b;
int c = Func(a);

b = 5;

C++

Much like C, C++ variables are declared at the very start of the program after the headers are declared. To declare a as an integer you say: the type of variable; then the variable followed by a semicolon ";".

int a;

From C++11, type of variables may be inferred:

auto a = 1;   // int a = 1

Template variables are specified with template parameters in angle brackets after the class name:

std::vector<int> intVec;

Caché ObjectScript

Variable type is not declared for local variables.

set MyStr = "A string"
set MyInt = 4
set MyFloat = 1.3

Array variables use a subscript as an element index.
The size is set automatically when a new element is added.
Each element can have an arbitrary number of sub elements.
Arrays are automatically sorted by subscript.

set MyArray(1) = "element 1"
set MyArray(2) = "element 2"
set MyArray(2,1) = "sub element 1 of element 2" 
set MyArray("Element 3") "element indexed by a string"
set MyArray = "Root element"

Global variables are persistent. They remain set even after reboot.
Global variables work the same as array variables.
Global variables are indicated with the '^' character.

set ^MyGlobal("a subscript") = "My Value"

Process private global variables exist for the lifetime of the process, can only be accessed by the process that instantiated it and are indicated as follows:

set ^||MyProcessPrivateGlobal("subscript 1") = "value"

ChucK

Much like C or C++, declared but not initialized:

 int a;

Multiple declaration:

int b,c,d;

Declared and initialized:

0 => int e;

Clojure

Clojure allows global variable declaration and initialization with def and the macros that expand into it (e.g. defn, defmacro). In Clojure, this is known as 'interning a var'. Clojure encourages immutable programming, but often does not enforce it: def and most of its related forms can be used successfully more than once with the same symbol in the same namespace, although this is considered bad style. A vast majority of real-world Clojure uses the def forms for global scope, and let-like bindings (loop, for, doseq) for local scope. Clojure provides options such as alter-var-root and with-redefs for more advanced use.

Declaration only:

(declare foo)
(def bar)

Global scope initialization:

(def foo 42)
(defonce bar 42)
(defn baz [x] 42)
(defmacro qux [x] 42)

Local scope initialization:

(let [foo 42] ...)

Local re-binding of dynamic global var:

(def ^:dynamic foo 10)
(binding [foo 20] ...)

Clojure is a dynamically typed language, but does support the use of type-hints in the form of metadata. This can improve performance when interop with the host language would otherwise cause reflection:

(defn len [^String x]
  (.length x))

For mutable programming, Clojure provides atoms, refs, and agents. These can be stored in a var to allow managed, thread-safe mutation. atoms are the most common way to store state, but refs and agents can be used when coordinated and asynchronous functionality is required, respectively.

(def foo (atom 42))
(def bar (ref 42))
(def baz (agent 42))

COBOL

For example usage of array variables in COBOL, see Arrays#COBOL.

Assignment

Variables can be assigned values using either MOVE or SET. SET is used for assigning values to indexes, pointers and object references, and MOVE is used for everything else.

MOVE 5 TO x
MOVE FUNCTION SOME-FUNC(x) TO y
MOVE "foo" TO z
MOVE "values 1234" TO group-item
SET some-index TO 5

One of COBOL's more well-known features is MOVE CORRESPONDING, where variables subordinate to a group item are assigned the values of variables with the same names in a different group item. The snippet below uses this to reverse the date:

01  normal-date.
    03  year     PIC 9(4).
    03  FILLER   PIC X VALUE "-".
    03  month    PIC 99.
    03  FILLER   PIC X VALUE "-".
    03  dday     PIC 99. *> Misspelling is intentional; day is a reserved word.

01  reversed-date.
    03  dday     PIC 99.
    03  FILLER   PIC X VALUE "-".
    03  month    PIC 99.
    03  FILLER   PIC X VALUE "-".
    03  year     PIC 9(4).
...
PROCEDURE DIVISION.
    MOVE "2012-11-10" TO normal-date
    MOVE CORR normal-date TO reversed-date
    DISPLAY reversed-date *> Shows '10-11-2012'

Declaration

Variables in COBOL are declared in the DATA DIVISION. In standard COBOL, they can be declared within it in:

  • the FILE SECTION, where sort-files/file records are defined and associated with their respective file/sort descriptions.
  • the WORKING-STORAGE SECTION, where static data is declared.
  • the LOCAL-STORAGE SECTION, where automatic data is declared.
  • the LINKAGE SECTION, where parameters are defined.
  • the REPORT SECTION, where reports are defined and associated with report descriptions.
  • the SCREEN SECTION, where the screens used for terminal I/O are described.

Variables are defined in the following format: level-number variable-name clauses.

Variable type is defined in a PICTURE clause and/or a USAGE clause. PICTURE clauses can be used like so:

01  a PIC X(20).      *> a is a string of 20 characters.
01  b PIC 9(10).      *> b is a 10-digit integer.
01  c PIC 9(10)V9(5). *> c is a decimal number with a 10-digit integral part and a 5-digit fractional part.
01  d PIC 99/99/99.   *> d is an edited number, with a slash between each pair of digits in a 6-digit integer.

The USAGE clause is used to define pointers, floating-point numbers, binary numbers, packed decimals and object references amongst others.

Each variable has a level-number, which is a number from 1 to 49, or 77, which goes before the variable name. Level-numbers indicate how data is grouped together. Variables with higher level-numbers are subordinate to variables with lower level-numbers. The 77 level-number indicates the variable has no subordinate data and is therefore not a group item. Group items can include FILLER items which are parts of a group item which are not directly accessible.

*> Group data items do not have a picture clause.
01  group-item.
    03  sub-data          PIC X(10).
    03  more-sub-data     PIC X(10).

Initialization

Initialization is done via the VALUE clause in the DATA DIVISION or via the INITIALIZE statement in the PROCEDURE DIVISION. The INITIALIZE statement will set a variable back to either the value in its VALUE clause (if INITIALIZE has a VALUE clause) or to the appropriate value out of NULL, SPACES or ZERO.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  initialized-data      PIC X(15) VALUE "Hello, World!".
01  other-data            PIC X(15).
...
PROCEDURE DIVISION.
    DISPLAY initialized-data *> Shows 'Hello, World!'
    DISPLAY other-data       *> Will probably show 15 spaces.

Group items can be initialized, but they are initialized with a string like so:

01  group-item   VALUE "Hello!12345".
    03  a-string PIC X(6). *> Contains "Hello!"
    03  a-number PIC 9(5). *> Contains '12345'.

Reference Modification

Reference modification allows a range of characters to be taken from a variable.

some-str (1:1)      *> Gets the first character from the string
some-num (1:3)      *> Get the first three digits from the number
another-string (5:) *> Get everything from the 5th character/digit onwards.

*> To reference modify an array element
some-table (1) (5:1)  *> Get the 5th character from the 1st element in the table

Scope

Variables by default are local to the subprogram/class/etc. (source element) they are defined in. The GLOBAL clause allows the variable to be accessed in any nested source units as well. To be accessed from those inner source elements, the variable must be redeclared exactly as it was in the outer one, complete with GLOBAL clause, otherwise the variable in the inner one will shadow the global variable from the outer one.

Common Lisp

Declaration

Special variables are more or less like globals in other languages: http://www.lispworks.com/documentation/HyperSpec/Body/d_specia.htm

Special variables may be defined with defparameter.

(defparameter *x* nil "nothing")

Here, the variable *x* is assigned the value nil. Special variables are wrapped with asterisks (called 'earmuffs'). The third argument is a docstring.

We may also use defvar, which works like defparameter except that defvar won't overwrite the value of the variable that has already been bound.

(defvar *x* 42 "The answer.")

It does, however, overwrite the docstring, which you can verify:

(documentation '*x* 'variable)

defconstant works in the same way, but binds a constant. Constants are wrapped with '+' signs rather than earmuffs.

Common Lisp is case-insensitive, so saying (equal +MoBy-DicK+ +moby-dick+) will return t no matter the combination of upper and lower-case letters used in the symbol names. Symbols are hyphenated and lower-case.

For local varibles, we use let:

(let ((jenny (list 8 6 7 5 3 0 9))
      hobo-joe)
  (apply #'+ jenny))

The symbols 'jenny' and 'hobo-joe' are lexically bound meaning that they are valid within the scope of the let block. If we move the apply form out of scope, the compiler will complain that 'jenny' is unbound.

The let macro binds an arbitrary number of variables: 'jenny' is bound to a list of numbers, whereas hobo-joe is bound to nil because we haven't provided a value. jenny and hobo-joe have the same scope.

Common Lisp prefers to use variables in lexical, rather than dynamic scope. If we've defined *x* as a special variable, then binding it lexically with let will create a new local value while leaving the dynamically scoped *x* untouched.

(progn
  (let ((*x* 43))
    (print *x*)
  (print *x*))

If *x* has previously been bound to the value 42, then this example will output 43, then 42.

Mutation

We use setf to modify the value of a symbol.

(setf *x* 625)

We can also modify multiple symbols sequentially:

(setf *x* 42 *y* (1+ *x*))
=>43

The return value is of the last form evaluated. In that example, *x* is referred to twice, and the value applied to *y* depends on the modified value of *x*. We can use psetf to set variables in parallel:

(setf *x* 625)
(psetf *x* 42 *y* (1+ *x*)
=>NIL

The return value is NIL, but the resulting value of *y* is 626 instead of 43.

Types

Common Lisp is dynamically typed, so we don't have to explicitly tell it what type of value a symbol holds. We nonetheless have the option of being explicit about which types our functions use through ftype declarations, which tell the compiler what to expect:

(declaim (ftype (function (fixnum) fixnum) frobnicate))
(defun frobnicate (x)
  (+ x 42))

In this example, the function frobnicate must be written to take one fixnum (i.e., a fixed-width integer as opposed to a bignum) and must return a fixnum. Having been given this type information, the compiler can now assert that the function works the way we've told it it does, and will throw an error when you've made a mistake in implementing or calling the function. The compiler may also use more performant fixnum-specific arithmetic functions instead of the generic arithmetic functions.

You can give the compiler the same information using in-function type declarations and the the operator:

(defun frobnicate (x)
  (declare (type fixnum x))
  (the fixnum (+ x 128)))

The declare statement applies to the function in which the statement appears. In the example, we assert to the compiler that x is a fixnum. In some compilers, the tells the compiler the type returned by an expression; we want our frobnicate to only ever return a fixnum and to throw an error if anything causes the return value to be anything other than a fixnum.

D

float bite = 36.321;	///_Defines a floating-point number (float), "bite", with a value of 36.321
float[3] bites;		///_Defines a static array of 3 floats
float[] more_bites;	///_Defines a dynamic array of floats

DBL

;
;       Variables examples for DBL version 4 by Dario B.
;

.DEFINE NR,10           ;const
.DEFINE AP,"PIPPO"      ;const


        RECORD CUSTOM
COD,    D5
NAME,   A80
ZIP,    D6
CITY,   A80
;-----------------------

        RECORD

ALPHA,  A5             ;alphanumeric
NUMBR,  D5             ;number
DECML,  F5.2           ;float
NUMVE,  10D5           ;array of number
NUMAR,  [10,2]D5       ;array of number
ALPV1,  10A8           ;array of alphanumeric
ALPV2,  [NR]A8         ;array of alphanumeric
ALPA1,  [10,2]A8       ;array of alphanumeric 

NUMV,   3D3,100,200,300
ALPV,   2A3,'ABC','FGH','KLM'
MSX,    A9,"VARIABLES"
MSG,    A*,'Esempio di variabile autodimensionante'

        PROC
;-----------------------------------------------------------------------

     CLEAR ALPHA,NUMBR,DEML,NUMVE(1:10*5),NUMAR(1:10*2*5),ALPV1(1:10*8)
     CLEAR ALPV2(1:10*8),ALPA1(1:10*2*8)
     
     ALPHA="PIPPO"
     NUMBR=10
     DECML=20.55

     CLEAR CUSTOM
     COD=1050
     NAME='Dario Benenati'
     ZIP=27100
     CITY="PAVIA"

     NUMVE(1:10*5)=
     NUMVE(1)=1
     SET NUMVE(2),NUMVE(3),NUMVE(4)=2
     
     NUMAR(1:10*2*5)=
     NUMAR[1,1]=11
     NUMAR[1,2]=12
     NUMAR[2,1]=21
     NUMAR[2,2]=22

     ALPV1(1:10*8)=
     ALPV1(1)="PIPPO"
     APLV1(2)="PLUTO"
     APLV1(2)="ABCDEFGHIJKLMNOP"       ;ALPV(3)='IJKLMNOP'

     ALPV2(1:10*8)="                   "
     ALPV2[1]="PIPPO"
     ALPV2(2)="PLUTO"
     ALPV2[3](3:2)="FO"
     ALPV2[4](3,4)="FO"

     SET ALPA1[1,1],ALPA1[1,2]="PLUTO"
     ALPA1[2,1](3:2)="FO"
     ALPA1[2,1](3,4)="FO"
     ;.....................

Delphi

var
  i: Integer;
  s: string;
  o: TObject;
begin
  i := 123;
  s := 'abc';
  o := TObject.Create;
  try
    // ...
  finally
    o.Free;
  end;
end;

DM

Types, procs (functions) and variables in DM are built with a very "tree" structure. Because of this, there are lots of ways to define variables. Fundamentally however, the "var" keyword is always used.

Declaring variables:

// Both of the following declarations can be seen as a tree,
// var -> <varname> 
var/x
var y

// They can also be defined like this.
// This is once again a tree structure, but this time the "var" only appears once, and the x and y are children.
var
    x
    y
// And like this, still a tree structure.
var/x, y

The type of a variable can be declared like this, note that there is are types for strings or numbers:

// Here, "/obj" is the type, a /obj being a simple game object.
var/obj/x
// It can also be defined like this, because of the tree structure:
var
    obj
        x
// Or this
var/obj
   x

// ...
// You get the idea

Variables can be assigned, during declaration or later. The compiler does not enforce types here at all.

var/x = 0
x = 10
var/y
y = "hi!"

Diego

Declaration

Declaration of variables is required by the caller with the original source of the variable. If a callee hears of a variable after a declaration, it can ask around or ask the caller for the declaration; ignore the variable and any instructions associated with it; or try to determine the context and/or the callers intentions. I suppose this depends upon the mood of the callee.

syntax:

Use add_var verb-object (short for 'add variable') or the dim action (short for 'dimension of'):

add_var(variablename); or dim(variablename);

...multiple variables can de declared in a comma separated list.

With no datatype specified, the variables are of variant type (as in basic'esque languages). Datatypes are specified using the _datatype_ posit (can be shortened to _dt):

add_var(variablename)_datatype(datatype); or dim(variablename)_dt(datatype);

...or using the {} braces:

add_var({datatype}, variablename); or dim({datatype}, variablename);

examples:

add_var(v1);
dim(v1);
add_var(v1, v2, v3, v4);
dim(v1, v2, v3, v4);
add_var(isRaining)_datatype(boolean);
dim(isRaining)_datatype(boolean);
add_var(greeting)_dt(string);
dim(greeting)_dt(string);
add_var({date}, birthdate);
dim({date}, birthdate);

Variables can also be declared at posit state. So, verb-object add_var becomes posits _addvar or, shortened, _var; action dim becomes a posit. So, for example, during a robot manoeuvre a variable can be declared:

go_robot(alif)_waypoint(waypoint1)_addvar({bool},alifArriveWaypoint1);
go_robot(beh)_waypoint(waypoint1)_var({bool},behArriveWaypoint1);
go_robot(teh)_waypoint(waypoint1)_dim({bool},tehArriveWaypoint1);

Initialisation

Initialisation of variables can exist at declaration (using posit _value or, the shortened _v) or after declaration (using with_var then _value). The first time the _value posit is used is the initialisation.

syntax:

add_var..._value(value);

dim..._v(value);


examples:

Initialisation at declaration:

add_var({integer},wholeNumber1,wholeNumber2)_value(3);
dim({double},realNumber)_v(3.0);
add_var({boolean},isRaining)_v(false);
add_var(greeting)_datatype(string)_value(Hello, this is World speaking.);

Initialisation after declaration:

with_var(birthdate)_value(24-Jun-1963);
[myinteger]_value(33);

Dynamic initialisation after declaration:

[myYearVar]_v(1963);
[datename]_v(24-Jun-[myYearVar]);

Variables that are not initialised are determined to be undefined. The callee can be sensitive to uninitialised (undefined) variables with its undefined sensitivity setting set_undefined()_sensiti();. So, for instance, if a callee is sensitive to a undefined variable, it will ask the caller and others in the mist for the declaration of the variable with command with_var(variablename)_askdec();.

Assignment

Initialisation and assignment are the same, since the first assignment is the initialisation. Direct assignment of variables is achieved, as with the initialisation, using _value (or shortened _v) posit after the with_var verb-object or [] variable referencing brackets, as shown above. Assignment of a variable with other variables and operators can be achieved using the _calc (short for 'calculation') posit. Within the brackets of _calc the square brackets [] is be used to reference variables.

syntax:

with_var(variablename)_calc(expression/calculation);

[variablename]_calc(expression/calculation);

examples:

with_var(d)_calc([b]^2-4*[a]*[c]);
with_var(E)_calc([m]*[c]^2);
with_var(d)_calc([b]^2-4*[a]*[c]);

Note, instead of using the _calc posit, all operators can be also be posits, for example:

with_var(E)_equals(m)_multipl(c)_sq();  // E=mc²
with_var(c)_inc();  // increment by one
with_var(c)_inc(a);  // same as with_var(c)_exp(+=[a]);


Datatypes

Datatypes in Diego are only implied by name following the general-purpose datatypes proposed in ISO/IEC 11404. The datatype given in Diego code is presumed to be the datatype of the caller. The callee will have to presume the datatype name from the caller is the same datatype of the same name by the callee.

syntax:

The syntax of declaring datatypes is achieved using the _datatype (or shortened _dt) posit.

add_var..._datatype(datatype)...

dim..._dt(datatype)...

A further shortened use of the {} brackets can be used inside declarations.

add_var({datatype},variablename);

dim({datatype},variablename);

examples:

add_var(wholeNumber)_datatype(integer);
dim({double},myNumber)_v(3.0);
with_var(wholenumber)_datatype(integer);
with_var({boolean},isRaining)_value(false); // cast/converting datatype

However, datatypes can be implied to be variables using verb-object and posit commands, as such:

syntax:

add_str(variablename)_value(value) to declare a string datatype.

with_bool(variablename)_value(value) to reference a boolean datatype, etc.

_float([variablename]=value) to declare an integer datatype in posit state.

_int([variablename]),variablename) to declare an integer datatype in posit state, etc.

examples:

add_str(bobName)_value(Bob);
alert_human(bob)_msg(Hi [bobName]);         // Hi Bob

alert_human(fred)_msg(Hi [fredName])_str(fredName)_value(Freddy);  // Hi Freddy

Scope

Variable scope is generally described a "public and interpreted" in Diego, as all variables are shared amongst thingys (humans, robots, and mobots). Every variable is 'public access', so scope is only achieved through discernment, swarmation, and discrimination. The keyword me (used as an verb-action, action, or, posit) is used to keep discernment of other thingys for use of the variable. The use of objects or criteria is used to discriminate against/for humans/robots/mobots. Then excluding objects (or using other criteria) is used to swarmate humans/robots/mobots.

For example:

add_var(greeting)_value(Hello World)_me();

or

with_me()_var(greeting)_value(Hello World);

This command will share the variable greeting and its value with all thingys, but, the other thingys that received this command will not act upon it because they know this variable is for the exclusive use of the caller (me). This is discernment.

However, the command...

add_var(greeting)_value(Hello World);

...will share and use the variable greeting and its value with all thingys that received this command. This is swarmation.

However, the command...

add_var(greeting)_value(Hello World)_for()_computer(cmp1);

...will share the variable greeting and its value with all thingys that received this command, but only the computer named cmp1 will use/act upon this variable. This is discrimination.

Scope also occurs within functions (_funct object in Diego); instructions (_instruct object in Diego); and, more, using the same approach with scope with variables.

There are also namespaces (using object _namespace, or, shortened, _ns) that contain the scope of variables, however, all variables are public inside the namespace, only differentiated between variables of the same signature (i.e. name and datatype).

Referencing

Referencing variables is achieved using the verb-object with_var, or implied with_var with the use of () and [] brackets, and combinations of.

When referencing variables the use of () brackets refers to the name of the variable. Using the [] brackets refers to value of the variable to be the variable name. Using the square brackets nested inside brackets, ([]), refers the to variable name which can be manipulated. Use of [[]] refers to the value to the variable name to the value of the variable to be the name of the variable.

For example:

add_var(a);                     // a = undefined variant
add_var({int},b);               // b = undefined integer 

with_var(a)_value(0);           // a = 0 variant
with_var({int},a)_v(1);         // a = 1 integer
with_var([a])_v(2);             // a = 2 integer

with_var[a]_v(3);               
// same as `with_var(3)_v(3);`
// callee will ask `with_var(3)_askdec();`

(a)_v(3);                       // a = 3 integer    // same as `add_var(a)_v(3);`
([a])_v(4);                     // a = 4 integer    // same as `add_var([a])_v(4);`

[a]_v(5);
// same as `with_var(3)_v(5);`
// callee will ask `with_var(3)_askdec();`

(a)_calc([a]+1);                // a = 5

add_var({str},varname)_v(a);
with_var[varname]_v(6);         // a = 6 integer
[varname]_inc();                // a = 7 integer    // same as `with_var(a)_inc();`

with_var([a]+1);                // a = 6 integer
with_var([a]+1)_v(7);           // a = 8 integer

with_var([[varname]]+1);        // a = 9 integer
with_var[[varname]]_inc();      // a = 10 integer
with_var([varname]+1);          // varname = "a1"

with_var({double},a)_v(11);     // a = 11.0 double

(varname)_v(a)_inc(2);          // varname = "c"    // same as `with_var(a)_v(a)_inc(2);`
with_var(varname)_v(b);         // varname = "b"
with_var[varname]_v(0);         // b = 0
([varname])_v(1);               // b = 1            // same as `with_var(b)_v(1);`

The default reference to a variable using [], refers to the adjacent variable reading from left to right, regardless of whether the variable is named or unnamed, for example:

alert_human(jo)_msg(Hi [])_str(joeName)_v(Joe);  // Hi Joe

alert_human(gab)_msg(Hi [])_str()_v(Gabriella);  // Hi Gabriella

alert_human(annamarie)_msg(Hi [])_str([a]+'-'[m])_str(a)_v(Anna)_str(m)_v(Marie);  // Hi Anna-Marie
alert_human(annamarie)_msg(Hi []+'-'+[])_str()_v(Anna)_str()_v(Marie);  // Hi Anna-Marie
alert_human(annamarie)_msg(Hi [])_str([]+'-'[])_str()_v(Anna)_str()_v(Marie);  // Hi Anna-Marie

Inside various posits, such as, _calc or _msg, the square brackets [] are used to escape into variables. Single quote marks can also be used to escape literals, but are optional.

For example:

alert_human(jill)_msg(Hi [jillName] Daniels)_var(jillName)_value(Jill);  // Hi Jill Daniels
alert_human(jill)_msg('Hi '+[jillName]+' Daniels')_var(jillName)_value(Jill);  // Hi Jill Daniels
add_var(jillFullName)_calc([jillName]+" Daniels");

DWScript

See Delphi for "classic" declaration. In DWScript, if variables have to be declared before use, but can be declared inline, and their type can also be inferred.

var i := 123;             // inferred type of i is Integer
var s := 'abc';           // inferred type of s is String
var o := TObject.Create;  // inferred type of o is TObject
var s2 := o.ClassName;    // inferred type of s2 is String as that's the type returned by ClassName

Dyalect

//A constant declaration
let pi = 3.14
 
private {
    //private constant, not visible outside of a module
    let privateConst = 3.3
}
 
//Variable declaration
var x = 42
 
//Assignment
x = 42.42
 
//Dyalect is a dynamic language, so types are attached
//to values, not to the names
var foo = (x: 2, y: 4) //foo is of type Tuple
var bar = "Hello!" //bar is of type String
 
//Global variable
var g = 1.1
 
{
    //local variable (not visible outside of { } brackets)
    var loc = 2.2
}
 
func fun() {
    //Local variables, not visible outside of function
    var x = 1
    var y = 2
}
 
func parent() {
    //A local variable inside a parent function
    var x = 1
    func child() {
        //A local variable inside a nested function
        //It shadows a parent's variable
        var x = 2
 
        //But this is how we can reference a variable from 
        //a parent function
        base.x
    }
}

E

E is an impure, lexically scoped language. Variables must be defined before use (they are not created on assignment). Definition of variables is a special case of pattern matching.

An identifier occurring in a pattern is a simple non-assignable variable. The def operator is usually used to define local variables:

def x := 1
x + x         # returns 2

Assignment

The pattern var x makes x an assignable variable, and := is the assignment operator.

def var x := 1
x := 2
x         # returns 2

(As a shorthand, var x := ... is equivalent to def var x := ....)

There are update versions of the assignment operator, in the traditional C style (+=, -=, |=, etc.), but also permitting any verb (method name) to be used:

def var x := 1
x += 1                # equivalent to x := x + 1, or x := x.add(1)
x                     # returns 2

def var list := ["x"]
list with= "y"        # equivalent to list := list.with("y")
list                  # returns ["x", "y"]

Patterns

Since variable definition is part of pattern matching, a list's elements may be distributed into a set of variables:

def [hair, eyes, var shirt, var pants] := ["black", "brown", "plaid", "jeans"]

However, assignment to a list as in Perl or Python is not currently supported.

[shirt, pants] := ["white", "black"]   # This does not do anything useful.

Scoping

In E, a variable is visible from the point of its definition until the end of the enclosing block. Variables can even be defined inside expressions (actually, E has no statement/expression distinction):

def list := [def x := timer.now(), x]    # two copies of the current time
list[0] == x                             # x is still visible here; returns true

Slots

The difference between assignable and non-assignable variables is defined in terms of primitive operations on non-primitive slot objects. Slots can also be employed by programmers for effects such as variables which have an effect when assigned (e.g. backgroundColor := red) or automatically change their values over time, but that is beyond the scope of this task. For example, it is possible to transfer a variable between scopes by referring to its slot:

def makeSum() {
  var a := 0
  var b := 0
  return [&a, &b, fn { a + b }]
}

def [&x, &y, sum] := makeSum()
x := 3
y := 4
sum() # returns 7

As suggested by the & syntax, the use of slots is somewhat analogous in effect to C pointers or C++ references, allowing the passing of locations and not their values, and "pass-by-reference" or "out" parameters:

def getUniqueId(&counter) {
  counter += 1
  return counter
}

var idc := 0
getUniqueId(&idc) # returns 1
getUniqueId(&idc) # returns 2

EasyLang

# it is statically typed
# 
# global number variable
n = 99
print n
# global array of numbers
a[] = [ 2.1 3.14 3 ]
# 
proc foo . .
   # i is local, because it is first used in the function  
   for i = 1 to len a[]
      print a[i]
   .
.
foo
# 
# string
domain$ = "easylang.dev"
print domain$
# 
# array of strings
fruits$[] = [ "apple" "banana" "orange" ]
print fruits$[]

Eiffel

Variables must be declared before their use with an explicit type.

class
	APPLICATION
inherit
	ARGUMENTS
create
	make

feature {NONE} -- Initialization

	make
			-- Run application.
		local
			i: INTEGER
			s: STRING
		do
			i := 100
			s := "Some string"
			create a.make_empty
		end

feature {NONE} -- Class Features

a: ARRAY[INTEGER]

In this example, i and s have local scope and a has global scope. Two variables of the same class cannot have the same name, regardless of scope. Global variables are considered class features and follow the same export status modifiers as normal features.

There are two types of variables, reference or expanded. Reference type variables refer either to an object of the declared datatype, or Void. Expanded type variables always correspond to an object. The basic objects are of expanded type. These include numbers, Booleans, and characters. All variables are objects, and are auto-initialized. For reference types, the initial value is Void. For expanded types, a default creation feature is called.

Variables can be initialized through a creation feature, such as for a in the example above.

Assignment to variables depends on the type of variable

-- assume A and B are of the same datatype
B := A
A.some_modification_feature

For reference type variables, B copies the reference of A, so any modifications to A (or B) affects the other. In the case of expanded types, B will copy A (memory-wise copy) so any modification to A (or B) will never be seen by the other.

Ela

Strictly speaking Ela doesn't have variables. Instead Ela provides a support for a declaration of names that can be bound to values. Unlike variables names are immutable - it is not possible to change a value bound to a name.

Global declarations:

x = 42

sum x y = x + y

Local declarations:

sum x y = let z = x + y in z

sum x y = z
          where z = x + y

Elena

ELENA 5.0:

import system'collections;

public program()
{
   var c := nil;                  // declaring variable.
   var a := 3;                    // declaring and initializing variables
   var b := "my string".Length;
   long l := 200l;                // declaring strongly typed variable  
   auto lst := new List<int>();     

   c := b + a;                    // assigning variable
}

Erlang

Variables spring into life upon assignment, which can only happen once (single assignment). Their scope is only the local function and they must start with a capital letter.

two() ->
	A_variable = 1,
	A_variable + 1.

F#

Variables in F# bind a name to a value and are, by default, immutable. They can be declared nearly anywhere and are normally local to the block/assembly they are defined in. They are declared in the form: let name parameters = expression.

let x = 5                              // Int
let mutable y = "mutable"              // Mutable string
let recordType = { foo : 6;  bar : 6 } // Record
let intWidget = new Widget<int>()      // Generic class
let add2 x = 2 + x                     // Function value

Types are normally inferred from the values they are initialised with. However, types can be explicitly specified using type annotations.

let intEqual (x : int) (y : int) = x = y
let genericEqual x y : 'a = x = y

Mutable variables are set using the <- operator.

sum <- sum + 1

Factor

The SYMBOL bit defines a new symbol word which is used to identify variables. use-foo shows how one would modify and get the contents of the variable. named-param-example is an example of using :: to define a word with named inputs, similar to the way other languages do things. Last, but not least, local-example shows how to use [let to define a group of lexically scoped variables inside of a word definition.

SYMBOL: foo

: use-foo ( -- )
    1 foo set
    foo get 2 + foo set ! foo now = 3
    foo get number>string print ;

:: named-param-example ( a b -- )
    a b + number>string print ;

: local-example ( -- str ) [let "a" :> b "c" :> a a " " b 3append ] ;

Falcon

/* partially created by Aykayayciti Earl Lamont Montgomery
April 9th, 2018 */

/* global and local scrope
   from the Falcon survival
   guide book */
// global scope
sqr = 1.41

function square( x )
	// local scope
   sqr = x * x
   return sqr
end


number = square( 8 ) * sqr


a = [1, 2, 3] 	// array
b = 1			// variable declaration
e = 1.0  		// float
f = "string"	// string

/* There are plenty more 
data types in Falcon */

Forth

Local Variables

Historically, Forth has preferred open access to the parameter stack over named local variables. The 1994 standard however added a cell-sized local variable facility and syntax. The semantics are similar to VALUEs: locals are initialized from stack contents at declaration, the name retrieves the value, and TO sets the value of the local name parsed at compile time ("value TO name").

: hypot ( a b -- a^2 + b^2 )
  LOCALS| b a |            \ note: reverse order from the conventional stack comment
  b b * a a * + ;
Works with: GNU Forth

Modern Forth implementations often extend this facility in several ways, both for more convenient declaration syntax and to be more compatible with foreign function interfaces. Curly braces are used to replace the conventional stack comment with a similar looking local variable declaration.

: hypot { a b -- a^2 + b^2 }      \ text between "--" and "}" remains commentary
  a a * b b * + ;

Modern systems may also allow different local data types than just integer cells.

: length { F: a F: b F: c -- len }     \ floating point locals
  a a F* b b F* F+ c c F* F+ FSQRT ;

Global Variables

As mentioned Forth normally uses the parameter stack for input/output arguments. When there is the need for a Global variable Forth simply creates a label and allocates a piece of memory to the variable. When the variable is invoked it does not return the value in the memory but rather it returns the address of the variable on the parameter stack. This is like what other languages call a pointer, however Forth has no such obfuscation. A named memory address is simple to understand. To store a value in the variable the '!' (store) operator is used and to fetch a value from a variable the '@' (fetch) operator is used. VARIABLEs have the same size as the processor's native integer with a minimum size of 16 bits. Double precision variables are created with the word 2VARIABLE that are used with corresponding 2@ and 2! operators.

VARIABLE X   999 X !                \ create variable x, store 999 in X
VARIABLE Y   -999 Y !               \ create variable y, store -999 in Y
2VARIABLE W  140569874. W 2!        \ create and assign a double precision variable

Test at the Forth Console

X @ . 999 ok
Y @ . -999 ok

X @ Y @ + . 0 ok
W 2@ D. 140569874 ok

Fortran

 program test
 implicit none

 integer :: i  !scalar integer 
 integer,dimension(10) :: ivec !integer vector
 real :: r !scalar real
 real,dimension(10) :: rvec !real vector
 character(len=:),allocatable :: char1, char2  !fortran 2003 allocatable strings

!assignments:

!-- scalars:
 i = 1
 r = 3.14

!-- vectors:
 ivec = 1 !(all elements set to 1)
 ivec(1:5) = 2

 rvec(1:9) = 0.0
 rvec(10) = 1.0

!-- strings:
 char1 = 'hello world!'
 char2 = char1   !copy from one string to another
 char2(1:1) = 'H'  !change first character

end program test

FreeBASIC


Variable declaration

Dim [Shared] As DataType <i>vble1</i> [, <i>vble2</i>, ...]

or

Dim [Shared] <i>vble1</i> As DataType [, <i>vble2</i> As DataType, ...]

example:

Dim As Integer n1, n2
Dim x As Double
Dim isRaining As Boolean
Dim As String greeting


Initialization

Dim variable As datatype = value
Dim var1,var2,... As datatype

example:

Dim wholeNumber1,wholeNumber2 as Integer = 3
Dim realNumber as Double = 3.0
Dim isRaining as Boolean = False
Dim greeting as String = "Hello, this is World speaking."
Dim longArray() As Long = {0, 1, 2, 3}
Dim twoDimensions (1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}


Assignment

variable = expression
          v = a     or     v => a
          d = b^2 - 4*a*c
          s3 = s1 & mid(s2,3,2)
variable <operator>= expression2
          c += a 
          c -= a 
          c *= a 
          c /= a 
          c \= a 
          c ^= a 
          c Mod= a 
          c Shl= n 
          c Shr= n 
          c &amp;= a 
          c And= n 
          c Eqv= n 
          c Imp= n 
          c Or= n 
          c Xor= n


Standard data types and limits.

Numeric Types

TypeSize (bits)FormatMinimum ValueMaximum Value
Byte 8signed integer-128+127
Ubyte 8unsigned integer0+255
Short 16signed integer-32768+32767
Ushort 16unsigned integer065535
Long 32signed integer-2147483648+2147483647
Ulong 32unsigned integer0+4294967295
Integer 32/64signed integer32bit: -2147483648,
64bit: -9223372036854775808
32bit: +2147483647,
64bit: +9223372036854775807
Uinteger 32/64unsigned integer0 32bit: +4294967295,
64bit: +18446744073709551615
Longint 64signed integer-9223372036854775808+9223372036854775807
Ulongint 64unsigned integer0+18446744073709551615
Single 32floating point+/-1.401 298 E-45 +/-3.402 823 E+38
Double 64floating point+/-4.940 656 458 412 465 E-324 +/-1.797 693 134 862 316 E+308
enums 32/64 signed integer32bit: -2147483648,
64bit: -9223372036854775808
32bit: +2147483647,
64bit: +9223372036854775807


String Types

TypeCharacter
Size (bytes)
Minimum Size
(in characters)
Maximum Size
(in characters)
String 1 032bit: +2147483647,
64bit: +9223372036854775807
Zstring 1 032bit: +2147483647,
64bit: +9223372036854775807
Wstring 032bit: +2147483647,
64bit: +9223372036854775807


Scope
By default the scope of a variable is local to the sub, function or module. Attribute Shared can modify these scopes.

Shared makes module-level variables visible inside Subs and Functions. If Shared is not used on a module-level variable's declaration, the variable is only visible to the module-level code in that file.


Common
Declares a variable which is shared between code modules. A matching Common statement must appear in all other code modules using the variable.

Common [Shared] <i>symbolname</i>[()] [AS DataType] [, ...]


Var
Declares a variable whose type is implied from the initializer expression.

Var [Shared] variable  = expression
 
Var variable As datatype
Var var1,var2,... As datatype

example:

Var   s2  =   "hello"     '' var-len string
Var   ii   =   6728       '' implicit integer
Var   id   =   6728.0     '' implicit double

FutureBasic

void local fn Variables
  dim a as long                            // dim var as type
  dim as long b                            // dim as type var
  long c                                   // 'dim as' is optional
  long x, y, z                             // declarate multiple vars in one line
  CFStringRef s1 = @"Alpha", s2 = @"Bravo" // declare and assign in same statement
  CFArrayRef array = @[s1,s2]              // declare and assign an array
  c = 123                                  // assign
  CGRect r = {20,20,120,32}                // declare and assign record (struct)
end fn

fn Variables

HandleEvents

GAP

# At top level, global variables are declared when they are assigned, so one only writes
global_var := 1;

# In a function, local variables are declared like this
func := function(n)
    local a;
    a := n*n;
    return n + a;
end;

# One can test whether a variable is assigned
IsBound(global_var);
# true;

# And destroy a variable
Unbind(global_var);

# This works with list elements too
u := [11, 12, , 14];
IsBound(u[4]);
# true
IsBound(u[3]);
# false
Unbind(u[4]);

GlovePIE

if var.end=0 then // Without the code being in this if statement, the code would keep executing until it were to stop.
var.end=1
// These variables, respectively, refer to an integer, a boolean, and a string.
var.variable1=3
var.variable2=True
var.variable3="Hi!"
// var.example1 now refers to a string object instead.
var.variable1="Bye!"
endif

Go

Simplest and most common

While Go is statically typed, it provides a “short variable declaration” with no type explicitly stated, as in,

x := 3

This is the equivalent of,

var x int // declaration
x = 3     // assignment

The technique of not stating the type is known as type inference, or duck typing. The right hand side can be any expression. Whatever type it represents is used as the type of the variable. More examples:

y := x+1                  // y is int, assuming declaration above
same := x == y            // same declared as bool
p := &same                // type of p is pointer to bool
pi := math.Floor(math.Pi) // math.Floor returns float64, so that is the type of pi

Nothing goes uninitialized

Variables declared without initializer expressions are initialized to the zero value for the type.

var x, y int // two variables, initialized to zero.
var p *int   // initialized to nil

Opposite C

While putting the variable before the type feels “backwards” to programmers familiar with certain other languages, it succinctly allows multiple variables to be declared with arbitrarily complex type expressions.

List syntax

Variables can be declared in a list with the keyword var used only once. The syntax visually groups variables and sets the declaration off from surrounding code.

var (
    x, y int
    s string
)

Multiple assignment

Multiple values can be assigned in a single assignment statement, with many uses.

x, y = y, x                 // swap x and y
sinX, cosX = math.Sincos(x) // Sincos function returns two values
// map lookup optionally returns a second value indicating if the key was found.
value, ok = mapObject[key]

Other kinds of local variables

Parameters and named return values of functions, methods, and function literals also represent assignable local variables, as in,

func increase (x int) (more int) {
    x++
    more = x+x
    return
}

Parameter x and return value more both act as local variables within the scope of the function, and are both assignable. When the function returns, both go out of scope, although the value of more is then returned as the value of the function.

While assignment of return values is highly useful, assignment of function parameters is often an error. Novice programmers might think that modifying a parameter inside the function will affect a variable used as an argument to the function call. It does not.

Method receivers also represent assignable local variables, and as with function parameters, assigning them inside the method is often a mistake.

Other common errors

Short declarations can involve multiple assignment, as in

x, y := 3, 4

But there are complications involving scope and variables already defined that confuse many programmers new to Go. A careful reading of the language specification is definitely in order, and a review of misconceptions as discussed on the mailing list is also highly recommended.

Programmers new to the concept of closures often fail to distinguish between assigning free and bound variables. Function literals in Go are closures, and a common novice error is to start multiple goroutines from function literals, and fail to understand that multiple goroutines are accessing the same free variables.

Haskell

You can define a variable at the top (module) level or in a where, let, or do construct.

foobar = 15

f x = x + foobar
  where foobar = 15

f x = let foobar = 15
      in  x + foobar

f x = do
    let foobar = 15
    return $ x + foobar

One particular feature of do notation looks like assignment, but actually, it's just syntactic sugar for the >>= operator and a unary lambda.

main = do
    s <- getLine
    print (s, s)

-- The above is equivalent to:

main = getLine >>= \s -> print (s, s)

Pattern matching allows for multiple definitions of the same variable, in which case each call uses the first applicable definition.

funkshun True  x = x + 1
funkshun False x = x - 1

foobar = funkshun True 5 + funkshun False 5   -- 6 + 4

case expressions let you do pattern-matching on an arbitrary expression, and hence provide yet another way to define a variable.

funkshun m = case foo m of
    [a, b]           -> a - b
    a : b : c : rest -> a + b - c + sum rest
    a                -> sum a

Guards are as a kind of syntactic sugar for if-else ladders.

signum x | x > 0     =  1
         | x < 0     = -1
         | otherwise =  0

A defintion can be accompanied by a type signature, which can request a less general type than the compiler would've chosen on its own. (Because of the monomorphism restriction, there are also some cases where a type signature can request a more general type than the default.) Type signatures are also useful even when they make no changes, as a kind of documentation.

dotProduct :: [Int] -> [Int] -> Int
dotProduct ns ms = sum $ zipWith (+) ns ms
-- Without the type signature, dotProduct would
-- have a more general type.

foobar :: Num a => a
foobar = 15
-- Without the type signature, the monomorphism
-- restriction would cause foobar to have a less
-- general type.

Since Haskell is purely functional, most variables are immutable. It's possible to create mutable variables in an appropriate monad. The exact semantics of such variables largely depend on the monad. For example, STRefs must be explicitly initialized and passed between scopes, whereas the implicit state of a State monad is always accessible via the get function.

HicEst

! Strings and arrays must be declared.
! Everything else is 8-byte float, READ/WRITE converts
  CHARACTER str="abcdef", str2*345, str3*1E6/"xyz"/
  REAL, PARAMETER :: named_constant = 3.1415
  REAL :: n=2, cols=4, vec(cols), mtx(n, cols)
  DATA vec/2,3,4,5/, mtx/1,2,3.1415,4,  5,6,7,8/

  named = ALIAS(alpha, beta, gamma) ! gamma == named(3)
  ALIAS(vec,n, subvec,2) ! share subvec and vec(n...n+1)
  ALIAS(str,3, substr,n) ! share substr and str(3:3+n-1)

  a = EXP(b + c)     ! assign/initialze a=1, b=0, c=0
  str = "blahblah"   ! truncate/expand if needed
  beta = "blahblah"  ! illegal

  CALL noArguments_noUSE   ! global scope SUBROUTINE
  CALL Arguments_or_USE(a) ! local scope SUBROUTINE
  t = func()               ! local scope FUNCTION

SUBROUTINE noArguments_noUSE() ! all global
  vec2 = $ ! 1,2,3,...
END

SUBROUTINE Arguments_or_USE(var) ! all local
  USE : vec                      ! use global object
  var = SUM(vec)
  t = TIME()         ! local, static, USEd by func()
END

FUNCTION func()                  ! all local
  USE Arguments_or_USE : t       ! use local object
  func = t
END

HolyC

Variables must be declared before use. The declaration of a variable, without assigning a value takes the form <typename> <variablename>;

U8 i;

Some common types are: I8, I64, U8, U64, F64.

It is possible to initialize variables with expressions having known values when they are defined. The syntax follows the form <typename> <variablename> = <initializing expression>;

U8 b1 = 8;
U8 b2 = b1 * 10;

Multiple variables may be defined in a single statement as follows:

U8 uint1, uint2, uint3;

To initialized a string:

U8 *str = "The HolyC Language";

Icon and Unicon

Icon/Unicon data types are implemented as type safe self-descriptive values and as such do not require conventional type declarations. See Introduction to Unicon and Icon about declarations

Declarations are confined to scope and use and include local, static, global, procedure parameters, and record definitions. Additionally Unicon has class definitions. Undeclared variables are local by default.

global gvar          # a global

procedure main(arglist)         # arglist is a parameter of main
local a,b,i,x                   # a, b, i, x are locals withing main
static y                        # a static (silly in main)

x := arglist[1]
a := 1.0
i := 10
b := [x,a,i,b]

# ... rest of program 
end

Icon

Unicon

This Icon solution works in Unicon.

J

val=. 0

J has two assignment operators. The =. operator declares, initializes, assigns, etc. a local variable. The =: operator does the same for a "global" variable.

fun =: 3 :0
  val1 =: 0
  val1 =. 2
  val2 =. 3
  val1, val2
)
   fun''
2 3
   val1
0
   val2
|value error

Note that the language forbids assigning a "global" value in a context where the name has a local definition.

fun1 =: 3 :0
  val3=. 0
  val3=: 0
)
   fun1''
|domain error

But the purpose of this rule is to help people catch mistakes. If you have reason to do this, you can easily set up another execution context.

fun2 =: 3 :0
  val4=. 0
  3 :'val4=:y' y
)
   fun2 ''

Variables are referred to by name, and exist in locales (which may be used as classes, closures or other stateful references).

FIXME (working on good illustrative examples that would make sense to someone used to different languages)

That said, it is possible and not uncommon to write an entire J application without using any variables (J has a functional, "point free" style of coding known as tacit). Names are optional (though often convenient). And, it can be possible to build code using names and then remove them using f. -- this is somewhat analogous to compiling code though the implementation of f. does not have to compile anything.

Java

Variables in Java are declared before their use with explicit types:

int a;
double b;
AClassNameHere c;

Several variables of the same type can be declared together:

int a, b, c;

Variables can be assigned values on declaration or afterward:

int a = 5;
double b;
int c = 5, d = 6, e, f;
String x = "test";
String y = x;
b = 3.14;

Variables can have scope modifiers, which are explained here.

final variables can only be assigned once, but if they are Objects or arrays, they can be modified through methods (for Objects) or element assignment (for arrays):

final String x = "blah";
final String y;
final double[] nums = new double[15];
y = "test";
x = "blahblah"; //not legal
nums[5] = 2.5; //legal
nums = new double[10]; //not legal
final Date now = new java.util.Date();
now.setTime(1234567890); //legal
now = new Date(1234567890); //not legal

JavaScript

Information lifted from Stack Overflow (credit to krosenvold and triptych)

Javascript uses scope chains to establish the scope for a given function. There is typically one global scope, and each function defined has its own nested scope. Any function defined within another function has a local scope which is linked to the outer function. It's always the position in the source that defines the scope.

An element in the scope chain is basically a Map with a pointer to it's parent scope.

When resolving a variable, javascript starts at the innermost scope and searches outwards.

// a globally-scoped variable
var a=1;

// global scope
function one(){
    alert(a); 
}

// local scope
function two(a){
    alert(a);
}

// local scope again
function three(){
  var a = 3;
  alert(a);
}

// Intermediate: no such thing as block scope in javascript
function four(){
    if(true){
        var a=4;
    }

    alert(a); // alerts '4', not the global value of '1'
}


// Intermediate: object properties
function Five(){
    this.a = 5;
}


// Advanced: closure
var six = function(){
    var foo = 6;

    return function(){
        // javascript "closure" means I have access to foo in here, 
        // because it is defined in the function in which I was defined.
        alert(foo);
    }
}()


// Advanced: prototype-based scope resolution
function Seven(){
  this.a = 7;
}

// [object].prototype.property loses to [object].property in the scope chain
Seven.prototype.a = -1; // won't get reached, because 'a' is set in the constructor above.
Seven.prototype.b = 8; // Will get reached, even though 'b' is NOT set in the constructor.



// These will print 1-8
one();
two(2);
three();
four();
alert(new Five().a);
six();
alert(new Seven().a);
alert(new Seven().b);

Joy

JOY does not have variables. Variables essentially name locations in memory, where values are stored. JOY also uses memory to store values, but has no facility to name these locations. The memory that JOY uses is commonly referred to as "the stack".

Initializing

The JOY stack can be initialized:

[] unstack

Assignment

Values can be pushed on the stack:

42

pushes the value 42 of type integer on top of the stack.

Stack

Calling the stack by name pushes a copy of the stack on the stack. To continue the previous example:

stack

pushes the list [42] on top of the stack. The stack now contains: [42] 42.

jq

jq variables are strictly speaking not variable: they are just names given to JSON values. For example, the expression "1 as $x | 2 as $x | $x " can be understood as assigning the value 1 to $x and then assigning another value to $x, with the final result being 2, but it must be understood that the second occurrence of $x shadows the first, so that the third occurrence is just a reference to the second. To see this more clearly, consider the following superficially similar expression:

jq -n '1 as $x | (2 as $x | $x) | $x'

In this case, the expression as a whole yields 1 (rather than 2 as previously), as the subexpression in parentheses does not cause shadowing of the first $x.

Naming and Assignment

In a jq program, variable names are strings beginning with "$" followed by one or more characters chosen from the set of alphanumeric characters augmented with the "_" (underscore) character. Assignment within a jq program is based on the syntax:

EXPRESSION as $NAME

This establishes $NAME as a reference to the value of EXPRESSION. There is no special syntax to constrain the type of a variable.

Global Variables

Global variables can be given values on the jq command line. For example, suppose the following two lines are in a file named test.jq:

def test: $x; 
test

The following invocation:

jq --arg x 123 -n -f test.jq

will result in the string "123" being output.

Julia

Certain constructs in the language introduce scope blocks, which are regions of code that are eligible to be the scope of some set of variables. The scope of a variable cannot be an arbitrary set of source lines, but will always line up with one of these blocks. The constructs introducing such blocks are:

function bodies (either syntax) while loops for loops try blocks catch blocks let blocks type blocks.

#declaration/assignment, declaration is optional
x::Int32 = 1
#datatypes are inferred dynamically, but can also be set thru convert functions and datatype literals
x = 1 #x is inferred as Int, which is Int32 for 32-bit machines, Int64 for 64-bit machines
#variable reference
julia>x
1

x = int8(1) #x is of type Int8
x = 1.0 #x is Float64
x = y = 1 #assign both x and y to 1
global x = 1 #assigns 1 to global variable x (used inside scope blocks)
local x = 1 #assigns 1 to local variable x (used inside scope blocks)
x = 'a' #x is a 'Char' type, designated by single quotes
x = "a" #x is a 1-element string, designated by double quotes

A common use of variables is giving names to specific, unchanging values. Such variables are only assigned once. This intent can be conveyed to the compiler using the const keyword:

const e  = 2.71828182845904523536
const pi = 3.14159265358979323846

Kotlin

Local variables in Kotlin (i.e. variables created within a function, constructor or a property getter/setter) must be individually declared and initialized before they are used. They then remain in scope until the end of the block in which they are declared though their lifetime may be extended if they are 'captured' by a closure of some kind.

A variable's type can either be inferred from the type of the initialization expression or can be declared explicitly. There are essentially two types of variables: read-only (declared with 'val') and mutable (declared with 'var').

Variables declared at top-level or class/object scope are technically properties rather than variables and only have hidden backing fields where necessary. However, like local variables, they are either read-only or mutable and (in the case of top level or object properties) can be declared to be compile-time constants using the 'const val' modifier.

Here are some examples of local variables:

// version 1.0.6

fun main(args: Array<String>) {
    /* read-only variables */
    val i = 3          // type inferred to be Int
    val d = 2.4        // type inferred to be double
    val sh: Short = 2  // type specified as Short
    val ch = 'A'       // type inferred to be Char
    val bt: Byte = 1   // type specified as Byte 

    /* mutable variables */
    var s = "Hey"      // type inferred to be String
    var l =  4L        // type inferred to be Long
    var b: Boolean     // type specified as Boolean, not initialized immediately
    var f =  4.4f      // type inferred to be Float 

    b = true           // now initialized 
    println("$i, $d, $sh, $ch, $bt, $s, $l, $b, $f")
           
    s = "Bye"          // OK as mutable
    l = 5L             // OK as mutable
    b = false          // OK as mutable
    f = 5.6f           // OK as mutable

    println("$i, $d, $sh, $ch, $bt, $s, $l, $b, $f")
}
Output:
3, 2.4, 2, A, 1, Hey, 4, true, 4.4
3, 2.4, 2, A, 1, Bye, 5, false, 5.6

Lambdatalk

1) working in a global scope

{def A 3}
-> A                                 // A   is in the global scope
{def B 4}
-> B                                 // B   is in the global scopel
{def MUL {lambda {:x :y} {* :x :y}}}
-> MUL                               // MUL is a global function
{MUL {A} {B}}                        // using global variables
-> 12

2) working in a local scope

{let                                 // open local scope
 {                                     // begin defining and assigning local variables
   {:a 3}                                // :a is local
   {:b 4}                                // :b is local 
   {:mul {lambda {:x :y} {* :x :y}}}     // :mul is a local function
 }                                     // end defining and assigning local variables 
   {:mul :a :b}                        // computing with local variables
}                                    // closing local scope
-> 12

Lasso

// declare thread variable, default type null
var(x)
$x->type // null

// declare local variable, default type null
local(x)
#x->type // null

// declare thread variable, initialize with a type, in this case integer
var(x = integer)

// declare local variable, initialize with a type, in this case integer
local(x = integer)

// assign a value to the thread var x
$x = 12

// assign a value to the local var x
$x = 177

// a var always has a data type, even if not declared - then it's null
// a var can either be assigned a type using the name of the type, or a value that is by itself the type
local(y = string)
local(y = 'hello')

'\r'
// demonstrating asCopyDeep and relationship between variables:
local(original) = array('radish', 'carrot', 'cucumber', 'olive')
local(originalaswell) = #original
local(copy)     = #original->asCopyDeep
iterate(#original) => {
    loop_value->uppercase
}
#original		// modified
//array(RADISH, CARROT, CUCUMBER, OLIVE)
'\r'
#originalaswell // modified as well as it was not a deep copy
//array(RADISH, CARROT, CUCUMBER, OLIVE)
'\r'
#copy			// unmodified as it used ascopydeep
//array(RADISH, CARROT, CUCUMBER, OLIVE)

Liberty BASIC

'In Liberty BASIC variables are either string or numeric.
'A variable name can start with any letter and it can contain both letters and numerals, as well as dots (for example: user.firstname).
'There is no practical limit to the length of a variable name... up to ~2M characters.
'The variable names are case sensitive.

'assignments:   -numeric variables. LB assumes integers unless assigned or calculated otherwise.
'Because of its Smalltalk heritage, LB integers are of arbitrarily long precision.
'They lose this if a calculation yields a non-integer, switching to floating point.
    i = 1
    r = 3.14

'assignments    -string variables. Any string-length, from zero to ~2M.
    t$    ="21:12:45"
    flag$ ="TRUE"

'assignments    -1D or 2D arrays
'A default array size of 10 is available. Larger arrays need pre-'DIM'ming.
    height( 3)          =1.87
    dim height( 50)
    height( 23)         =123.5
    potential( 3, 5)    =4.5
    name$( 4)           ="John"

'There are no Boolean /bit variables as such.

'Arrays in a main program are global.
'However variables used in the main program code are not visible inside functions and subroutines.
'They can be declared 'global' if such visibility is desired.
'Functions can receive variables by name or by reference.

Lingo

In Lingo (local) variables are declared by assigning a value:

x = 23
y = "Hello world!"
z = TRUE -- same effect as z = 1

Trying to use a non declared variable causes a (pre)compile-time error. The following script doesn't compile, but throws "Script error: Variable used before assigned a value":

on foo
  put x
end

Lingo's value function can be used to "silently" - i.e. without throwing errors - check if a variable is defined or not in the current scope:

put value("x")
-- <Void> -- means: undefined

But variable declarations/assignments can also explicitely assign the constant VOID. The following script compiles successfully, but value("x") would still return <Void>:

on foo
  x = VOID
  put x
end

Global variables are declared by adding "global <varName>" either to the top of a script or anywhere - but before first usage - inside a function body. Both of the following scripts are valid and compile:

global x
on foo
  put x
end
on foo
  global x
  put x
end

Global variables can also be declared/created at runtime, by adding them as property to the special _global object:

_global.x = 23

Any other function/script can then access this dynamically created global variable, either by a having a "global <varName>" statement in its code (see above), or by using this special _global object:

put _global.x

Historically, Logo only had global variables, because they were easier to access when stepping through an algorithm. Modern variants have added dynamic scoped local variables.

Works with: UCB Logo
make "g1 0
name 2 "g2    ; same as make with parameters reversed
global "g3     ; no initial value
to func :x
  make "g4 4   ; still global
  localmake "L1 6
  local ["L2 "L3]    ; local variables, collection syntax
  func2 :g4
  print :L2      ; 9,  modified by func2
  print :L3      ; L3 has no value, was not modified by func2
end
to func2 :y
  make "g3 :y
  make "L2 :L1 + 3     ; dynamic scope: can see variables of callers
  localmake "L3 5       ; locally override L3 from caller
  (print :y :L1 :L2 :L3)      ; 4 6 9 5
end
print :g4   ; 4
print :L1   ; L1 has no value
print name? "L1    ; false, L1 is not bound in the current scope

LotusScript

Sub Click()
'a few declarations as example
Dim s as New NotesSession ' declaring a New NotesSession actually returns the current, active NotesSession
Dim i as Integer ' i = 0
Dim s as String ' s= ""
Dim v as Variant ' v is nothing
Dim l as Long ' l = 0
Dim doc as NotesDocument 'doc is EMTPY

'...

End Sub

Lua

In lua, variables are dynamically typecast, and do not need declaration prior to use.

a = 1    -- Here we declare a numeric variable
fruit = "banana"    -- Here we declare a string datatype
needspeeling = True    -- This is a boolean
local b = 2    -- This variable declaration is prefixed with a scope modifier

The lua programming language supports multiple assignments from within a single statement:

A, B, C, D, E = 2, 4, 6, 8, "It's never too late"

M2000 Interpreter

Each module has own local variables and can create global variables. A local variable shadow any global with same name. A new global with same name shadow a global variable (but its wrong to create a local first with same name, we see local always). We can change global variables, but to assign to a global variable we need to use <=. Arrays defined with DIM and global arrays not use <= for assign values to items.

Variables can exist in Groups as public or private. Also variables can be closures in lambda functions.

We can use Local to make local variables, Let to make variables, Def to make once variables (second time using Def for same variable we get error). We can use Input and Read to make new variables too. We can assign a value to new name, to make a variable.


\\ M2000 use inference to state the type of a new variable, at run time
\\ We can use literals of a numeric type
\\ @ for Decimal
\\ # for Currency
\\ ~ for Single
\\ & for Long (32bit)
\\ % for Integer (16bit)
\\ Double and Boolean have no symboles
Module TopA {
      Module Alfa {
                  Print A=10000, Type$(A)="Double"
                  \\ A is local, we use =
                  A=10@
                  Print A=10, Type$(A)
                  \\ Or we can state the type before
                  Def Currency K, Z=500, M
                  K=1000
                  \\ Currency Currency Currency
                  Print Type$(K), Type$(Z), Type$(M)
                  Def Double K1, K2 as Integer=10, K3=1
                  \\ double integer double
                  Print Type$(K1), Type$(K2), Type$(K3)
                  Mb=1=1
                  \\ We get a boolean
                  Print Type$(Mb)
                  Def boolean Mb1=True
                  Print Type$(Mb1)
                  \\ True and False are Double -1 and 0 not Boolean
                  Mb3=True
                  Print Type$(Mb3)="Double"
                  \\ For strings we have to use $ (like in old Basic)
                  A$="This is a String"
                  Global G1 as boolean = True
                  \\ To change a global variable we have to use <=
                  G1<=1=0
                  \\ If we do this: G1=1=0 we make a local variable, and shadow global
                  \\ In a For Object {} we can make temporary variables
                  For This {
                        Local G1=122.1212   
                        Print G1, Type$(G1)="Double"
                  }
                  Print G1, Type$(G1)="Boolean"
      }
      \\ shadow A for this module only
      A=100
      \\ Now we call Alfa
      Alfa
      Print (A=100)=True
}
Global A=10000
TopA
Print A=10000
Module CheckStatic {
      \\ clear static variables and variables (for this module)
      Clear
      Module K {
            \\ if no A exist created with value 100@
            \\ Static variables can't be referenced
            Static A=100@
            Print A, Type$(A)="Decimal"
            A++
      }
      For i=1 to 10 : K : Next i
      Print A=10000
}
CheckStatic 
Print A=10000

\\ reference and use of stack of values
C=100&
Module ChangeC {
      \\ we leave arguments in stack of values
      Module HereChangeC (&f) {
            \\ interpreter execute a Read &f
            f++
      }
      \\ now we call HereChangeC passing current stack of values
      HereChangeC
}
\\ Calling a module done without checking for what parameters a module take
ChangeC &C
Print C, Type$(C)="Long"
K=10010001001@
ChangeC &K
Print K, Type$(K)="Decimal"
Module TypeRef (&x as Double) {
     Print x
     x++
}
D=100
TypeRef  &D
Try ok {
      TypeRef &K
}
If Error or Not Ok then Print Error$ ' we get wrong data type

Maple

Variables are dynamic in Maple, so they do not need to be defined before they are used.

The assignment operator in Maple is := . It is also possible to assign to a variable name using the assign() command. It can also be noted that variables at the top level are global for a given session.

a := 1:
print ("a is "||a);
                            "a is 1"


Variables in a procedure should be declared with the "local" or "global" keyword.

A local variable has a smaller scope and can only be used within the procedure it's declared in. In the following example, b is local to f() and will be 3 within the procedure only. The local variable b does not have a value outside the procedure.

b;
f := proc()
    local b := 3;
    print("b is "||b);
end proc:
f();
print("b is "||b);
                               b
                            "b is 3"
                            "b is b"

A global variable has a large scope and can be used anywhere inside a program. A global variable declared outside a procedure can be used within the procedure, and a global variable declared within a procedure can be used outside of it. In the following example, a is a global variable that is from outside the procedure. The global variable c has a value even when used outside the procedure it was declared in.

f := proc()
    global c;
    c := 3;
    print("a is "||a);
    print("c is "||c);
end proc:
f();
print("c is "||c);
                            "a is 1"
                            "c is 3"
                            "c is 3"


Variables can be reassigned.

print ("a is "||a);
a := 4:
print ("a is "||a);
                            "a is 1"
                            "a is 4"


Variables can be reassigned as different datatypes.

print ("a is "||a);
type(a, integer);
a := "Hello World":
print ("a is "||a);
type(a, integer);
type(a, string);
                            "a is 4"
                              true
                       "a is Hello World"
                             false
                              true


Variable names may contain spaces and other characters when you enclose them in ``.

`This is a variable` := 1:
print(`This is a variable`);
                               1


To unassign a variable.

print ("a is "||a);
type(a, string);
print("c is "||c);
a := 'a':
print ("a is "||a);
type(a, symbol);
unassign('c');
print("c is "||c);
                       "a is Hello World"
                              true
                            "c is 3"
                            "a is a"
                              true
                            "c is c"

Here, symbol is essentially another term for variable name.

Mathematica/Wolfram Language

x=value	assign a value to the variable x
x=y=value	assign a value to both x and y
x=. or Clear[x]	remove any value assigned to x

lhs=rhs (immediate assignment)	rhs is evaluated when the assignment is made
lhs:=rhs (delayed assignment)	rhs is evaluated each time the value of lhs is requested


Atomic Objects

All expressions in Mathematica are ultimately made up from a small number of basic or atomic types of objects. 

Symbol / String / Integer / Real / Rational / Complex

These objects have heads which are symbols that can be thought of as "tagging" their types. 
The objects contain "raw data", which can usually be accessed only by functions specific to the particular type of object. 
You can extract the head of the object using Head, but you cannot directly extract any of its other parts.


Symbols are the basic named objects in Mathematica

aaaaa	user-defined symbol
Aaaaa	system-defined symbol
$Aaaa	global or internal system-defined symbol
aaaa$	symbol renamed in a scoping construct
aa$nn	unique local symbol generated in a module


Contexts

aaaa`x is a symbol with short name x, and context aaaa. 
Contexts in Mathematica work somewhat like file directories in many operating systems. 
You can always specify a particular file by giving its complete name, including its directory. 
But at any given point, there is usually a current working directory, analogous to the current Mathematica context. 
Files that are in this directory can then be specified just by giving their short names.


Scoping Constructs

With[] evaluate with specified variables replaced by values 
Module[] localize names of variables (lexical scoping)
Block[] localize values of variables (dynamic scoping)
DynamicModule[] localize names of variables in dynamic interface constructs

Other Forms of Scoping
Begin, End  localize symbol namespace
Throw, Catch  localize exceptions
Quiet, Check localize messages
BlockRandom localize pseudorandom variables

MATLAB / Octave

	a = 4; % declare variable and initialize double value,  
        s = 'abc'; % string 
        i8 = int8(5);	% signed byte 
        u8 = uint8(5);	% unsigned byte
        i16 = int16(5);	% signed 2 byte 
        u16 = uint16(5); % unsigned 2 byte integer
        i32 = int32(5);	% signed 4 byte integer
        u32 = uint32(5);% unsigned 4 byte integers 
        i64 = int64(5);	% signed 8 byte integer
        u64 = uint64(5);% unsigned 8 byte integer
	f32 = float32(5); % single precision floating point number 
	f64 = float64(5); % double precision floating point number , float 64 is the default data type. 

	c = 4+5i; %	complex number
        colvec = [1;2;4];   % column vector 
        crowvec = [1,2,4];   % row vector 
        m = [1,2,3;4,5,6];  % matrix with size 2x3

Variables within functions have local scope, except when they are declared as global

   global b

Mercury

Variables are normally implicitly instantiated into their natural scope, if not referred to in the head of the rule (as in Name below) and can only be bound once. This feels very similar to Erlang, where variables are function-scoped and single-assignment, without declaration. However, Mercury will reorder code to satisfy data dependencies, so assignments can appear to be out of order:

:- func name = string.
name = Name :-
    Name = Title ++ " " ++ Given,
    Title = "Dr.",
    Given = "Bob".

Mercury also has state variables, actually pairs of variables, usually instantiated in the head of a rule.

:- pred greet(string::in, io::di, io::uo) is det.
greet(Who, !IO) :-
    io.write_string("Hello", !IO),
    io.format(", %s!\n", [s(Who)], !IO).

In this example !IO is the state variable, and this code translates to

:- pred greet(string::in, io::di, io::uo) is det.
greet(Who, !.IO, !:IO) :-
    io.write_string("Hello", !.IO, !:IO),
    io.format(", %s!\n", [s(Who)], !.IO, !:IO).

which translates roughly to:

:- pred greet(string::in, io::di, io::uo) is det.
greet(Who, IO0, IO) :-
    io.write_string("Hello", IO0, IO1),
    io.format(", %s!\n", [s(Who)], IO1, IO).

!.IO is the bound 'current value' of IO, !:IO is the free 'next value' of IO, and the lexical appearance of these variables matters to their final translation to the normal variables in the third example, where data dependency enforces the intended order of operation (so that "Hello" is always written before the name.)

When state variables are instantiated within a rule, they need explicit instantiation, which you could think of as like a declaration without initialization. The state variable in the following example is existentially quantified, and is used for a temporary string builder:

:- func dr(string) = string.
dr(Name) = Titled :-
    some [!SB] (
        !:SB = string.builder.init,
        put_char(handle, 'D', !SB),
        put_char(handle, 'r', !SB),
        format(handle, " %s", [s(Name)], !SB),
        Titled = to_string(!.SB)
    ).

Existential vs. universal quantification comes up again to let you make local use of nondeterministic code, as in the following use of a nondeterministic list.member/2.

    % all_under(N, L)
    % True if every member of L is less than N
    %
:- pred all_under(int::in, list(int)::in) is semidet.
all_under(Limit, L) :-
    all [N] (member(N, L) => N < Limit).

In pure Mercury code that only uses functional data types, and only normal functions in higher-order code, variables can be said to be either 'free' or 'ground' where they're found in code, before the goal they're in changes their instantiation (for example to bind a free variable to a value, making it ground). The only exception are the 'state of the world' values used for I/O, which have 'unique' and 'dead' instantiations.

But as soon as you have higher-order predicates or non-functional data types (and this happens very easily even in rookie code, for example if you use the getopt module to handle command line arguments), then instantiations get more complex. For non-functional data types like arrays this might just mean using some special modes in your rules, but here's an extreme example:

:- type accumulator == (pred(int, int, io, io)).
:- inst accumulator == (pred(in, out, di, uo) is det).

:- func accumulator >> accumulator = accumulator.
:- mode in(accumulator) >> in(accumulator) = out(accumulator).
A >> B = C :-
    C = (pred(!.N::in, !:N::out, !.IO::di, !:IO::uo) is det :-
        A(!N, !IO),
        B(!N, !IO)).

:- pred add(int::in, int::in, int::out, io::di, io::uo) is det.
add(N, !Acc, !IO) :-
    io.format("adding %d to accumulator (current value: %d)\n",
        [i(N), i(!.Acc)], !IO),
    !:Acc = !.Acc + N.

:- pred example2(io::di, io::uo) is det.
example2(!IO) :-
    F = add(5)
        >> add(5)
        >> add(-10),
    F(0, X, !IO),
    io.print_line(X, !IO).
Output:
adding 5 to accumulator (current value: 0)
adding 5 to accumulator (current value: 5)
adding -10 to accumulator (current value: 10)
0

In that last example all of the insts and modes are just there to make the higher-order code work, so this system might seem like only a chore. It helps though in permitting efficient code with uniqueness, and it lets Mercury not always pay the costs of its logical facilities (there's underlying machinery required for a nondet call, which might be backtracked through, which isn't required in a det call). The variable instantiation system can also be used to provide static guarantees similar to those offered by dependently typed languages; an example of this is the non_empty_list instantiation used by the solutions module. This is getting really advanced, but here's a simple example of instantiation (ab)use:

:- module evenodd.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module list, string.

:- type coin
    --->    heads
    ;       tails.

:- inst heads for coin/0
    --->    heads.
:- inst tails for coin/0
    --->    tails.

main(!IO) :-
    (if heads(heads, N) then
        print_heads(N, !IO)
    else true).

:- pred print_heads(coin::in(heads), io::di, io::uo) is det.
print_heads(_, !IO) :-
    io.write_string("heads\n", !IO).

:- pred print_tails(coin::in(tails), io::di, io::uo) is det.
print_tails(_, !IO) :-
    io.write_string("tails\n", !IO).

:- pragma foreign_export_enum("C", coin/0, [prefix("COIN_"), uppercase]).

:- pred heads(coin::in, coin::out(heads)) is semidet.
:- pragma foreign_proc("C",
    heads(N::in, M::out(heads)),
    [promise_pure, will_not_call_mercury],
"
    M = N;
    SUCCESS_INDICATOR = N == COIN_HEADS;
").

Here, print_heads and print_tails take, by type, a coin, which could be either heads or tails, but this code has used the variable instantiation system to insist that they can each only accept one particular value. If you take this code and change the main/2 so that it instead tries to call print_tails(N, !IO), you'll get a compile-time error. A practical use of this is to avoid having to handle impossible (but well-typed) cases.

MIPS Assembly

Depending on your assembler, variables are either declared in a .data section, or you can use .equ or .definelabel directives to name a specific memory offset. The CPU doesn't (and can't) know the variable's size at runtime, so you'll have to maintain that by using the instructions that operate at the desired length.

foo equ 0x100 
bar equ foo+1               
;it's heavily implied that "foo" is one byte, otherwise, why would you label this? But you should always comment your variables.

Initialization is done by setting the variable equal to a value. For variables that live entirely in a register, this is very simple:

li $t0,5 ;load the register $t0 with the value 5.

For memory, this is a bit harder.

.definelabel USER_RAM,0xA0000000
foo equ 0x100 ;represents an offset from USER_RAM. Indexed offsetting can only be done at compile time in MIPS.

la $t0,USER_RAM
nop ;load delay slot ($t0 might not be ready by the time the load occurs)
lbu $t1,foo($t0)  ;load the unsigned byte from memory address 0xA0000100

Variables have no scope, and there is no enforcement of data types. There's nothing stopping you from loading a 32-bit word from an offset that was intended to store a byte. Do so at your own risk.

Modula-3

MODULE Foo EXPORTS Main;

IMPORT IO, Fmt;

VAR foo: INTEGER := 5; (* foo is global (to the module). *)

PROCEDURE Foo() =
  VAR bar: INTEGER := 10; (* bar is local to the procedure Foo. *)
  BEGIN
    IO.Put("foo + bar = " & Fmt.Int(foo + bar) & "\n");
  END Foo;

BEGIN
  Foo();
END Foo.

For procedures, the formal parameters create local variables unless the actual parameter is prefixed by VAR:

PROCEDURE Foo(n: INTEGER) =

Here, n will be local to the procedure Foo, but if we instead wrote:

PROCEDURE Foo(VAR n: INTEGER) =

Then n is the global variable n (if it exists).

Nanoquery

// variable definition and initialization/assignment
//
// variables are initialized when they are defined
a = 1
b = 2
c = null

// datatypes
//
// variables may contain any datatype, and this can be
// changed at any time by assigning a new value. they may
// also contain native java types
var = "a string"
var = 123
var = new(Nanoquery.Util.Random)
var = new(java.lang.Object)

// scope
//
// all classes, functions, and control blocks have their
// own scope
x = 5
for i in range(1, 2)
        // we can reference 'x' within this loop without issue
        println x

        // we can also define variables that have this loop as their
        // scope
        y = 5
end
// trying to reference the variable 'y' outside of its scope would result
// in a null reference exception
println y
//%null reference exception: variable object 'y' has not been defined
//   at <global>:33


// referencing
//
// as already demonstrated, variables are referenced by using their
// name
part1 = "this is "
part2 = "a test sentence"
println part1 + part2

// other facilities
//
// variables may be marked for garbage collection in their current
// scope using the 'delete' command
test_var = "pretend this is a really large object we want to free up"
delete test_var

Nim

1) Declaration Nim variables are declared with the keyword var for mutable variables and let for immutable variables. The declaration may be done at the module level (global variables) or in procedures, iterators, methods. At module level, when the name of a variable is followed by an asterisk (*) the variable is considered as exported and will be visible in other modules which import the declaration module.

It is possible to declare several variables in the same declaration statement.

2) Initialization

All variables are implicitly initialized to binary zero (which includes data allocated on the heap as strings and sequences). An initialization value may, of course, be specified.

When declaring a variable, the type must be specified, except if the variable is initialized as the type is determined from the initialization value. Immutable variables must be initialized at their declaration point.

If there is more than one variable initialized in the declaration statement, the expression is evaluated several times (this is important if the initialization expression contains a call to a procedure causing side-effects).

3) Assignment

Variables are assigned using the statement variable = expression. For structured objects, assigning causes generally a copy. There exists some mechanisms to avoid the copy, but they should be used with care.

4) References and pointers

Nim allows to declare references and pointers, for instance var p: ref int. Such variables are implicitly initialized to binary zero which is the value nil. For references, the allocation of data is done with the procedure new or implicitly in some cases. For pointers, there are several procedures similar to C allocation functions, for instance alloc or alloc0.

4) Scope

Global variables may be accessed from anywhere provided they are visible. To make a variable from another module visible, the module must be imported or, at least, the variable form the module must be imported. To access the field of an object, these fields must have been exported (using an asterisk as for exportation of a variable).

In a procedure “p”, local variables are visible from procedures declared inside “p”. However, Nim may prevent some operations on variables from an outside scope if they could result in a violation of the memory integrity.

5) Referencing

Variables may be referenced by their name or by their qualified name. The qualified name is the name preceded by the module name and a dot, for instance m.x.

Data managed via a reference or a pointer “p” may be accessed using the notation p[]. If the accessed data is an object containing some field “a” the short notation p.a may be used instead of the full notation p[].a.

6) Examples

var x: int = 3                  # Declaration with type specification and initialization. 

var y = 3                       # Declaration with type inferred to "int".

var z: int                      # Variable is initialized to 0.

let a = 13                      # Immutable variable.

# Using a var block to initialize.
var
  b, c: int = 10                # Two variables initialized to 10
  s* = "foobar"                 # This one is exported.

type Obj = ref object
  i: int
  s: string

var obj = Obj(i: 3, s: "abc")   # Initialization with an implicit allocation by "new".
echo obj.a, " ", obj.s          # Equivalent to obj[].a and obj[].s.

proc p =
  var xloc = 3
  echo x                        # Referencing a global variable.

  proc q =
    echo xloc                   # Referencing a variable in the enclosing scope.

Objeck

Different ways to declare and initialize an integer.

a : Int; 
b : Int := 13; 
c := 7;

OCaml

The default handlers for values in OCaml are not variables strictly speaking, because as OCaml is a functional language these values can't vary (so are not variable). Strictly speaking these are bindings. An identifier is bound to a value in an immutable way.

The standard way to bind an identifier to a value is the let construct:

let x = 28

This stated, ocaml programmers most often use the word variable when they refer to bindings, because in the programming world we usually use this word for the default values handlers.

Now to add confusion, real variables also exist in OCaml because it is an impure functional language. They are called references and are defined this way:

let y = ref 28

References can then be accessed and modified this way:

  !y       (* access *)
  y := 34  (* modification *)

An identifier can not be declared uninitialised, it is always defined with an initial value, and this initial value is used by the OCaml type inference to infer the type of the binding.

Inside an expression, bindings are defined with the let .. in construct, and we can also define multiple bindings with the let .. and .. in construct (here the expression can be the definition of a new identifier or the definition of a function):

let sum =     (* sum is bound to 181 *)
  let a = 31
  and b = 150 in
  (a + b)

let sum () =  (* sum is a function which returns 181 *)
  let a = 31
  and b = 150 in
  (a + b)

Oforth

Oforth does not have :

- Global variables.

- Class attributes.

Apart from the data stack that is global to a task, a function or method cannot modify something global.

Variables are local to functions or methods that declare them. They are a list of names between two pipes, declared at the beginning of a body. They are always initialized with null value.

Using a variable just push its value on the data stack. Using -> removes the top of the stack and assign this value to the variable

import: date

: testVariable
| a b c |
   Date now ->a
   a println ;

ooRexx

While REXX (and ooRexx) normally pass arguments by value. ooRexx has a mechanism to pass compound variables by reference.

a.=4711
Say 'before sub a.3='a.3
Call sub a.
Say ' after sub a.3='a.3
Exit
sub: Procedure
use Arg a.
a.3=3
Return
Output:
before sub a.3=4711
 after sub a.3=3

Ol

Ol is a purely functional Lisp, so there are no variables in common sense in Ol. We use 'symbol' and 'linked value' instead.

; Declare the symbol 'var1' and associate number 123 with it.
(define var1 123)
(print var1)
; ==> 123

; Reassociate number 321 with var1.
(define var1 321)
(print var1)

; Create function that prints value of var1 ...
(define (show)
   (print var1))
; ... and eassociate number 42 with var1.
(define var1 42)

(print var1)
; ==> 42
; var1 prints as 42, but...
(show)
; ==> 321
; ... function 'show' still print old associated value

Other possible ways to declare a symbol and link value with it: let, let*, letrec, letrec*, define a function. ... tbd...

Openscad

mynumber=5+4; // This gives a value of nine

OxygenBasic

'WAYS OF DECLARING VARIABLES
'AND ASSIGNING VALUES
var int a,b,c,d
int a=1,b=2,c=3,d=4
dim int a=1,b=2, c=3, d=4
dim as int a=1,b=2, c=3, d=4
dim a=1,b=2, c=3, d=4 as int
print c '3

'CREATING ARRAY VARIABLES
int array[100]
dim int a={10,20,30,40} 'implicit array
print a[3] '30

'COMMONLY USED TYPES:
'byte word int sys float char string

'LIMITING SCOPE
dim int a=10
scope
  dim int a=100
  dim int b=1000
  print a '100
end scope
print a 'prior a: 10
print b 'error b is out of scope

'REFERENCING VARIABLES
dim int*p 'p is a pointer variable
dim int a={2,4,6,8}
@p=@a[3] 'address of p is assigned address of a[3]
print @p 'the address
print p '6
print p[1] '6
print p[2] '8

Oz

Variable names in Oz always start with an uppercase letter.

Oz variables are dataflow variables. A dataflow variable can basically be free (unbound) or determined (has a value). Once a value has been assigned, it can not be changed. If we assign the same value again, nothing happens. If we assign a different value to an already determined variable, an exception is raised:

declare
Var         %% new variable Var, initially free
{Show Var}
Var = 42    %% now Var has the value 42
{Show Var}
Var = 42    %% the same value is assigned again: ok
Var = 43    %% a different value is assigned: exception

In the Emacs-based interactive environment, declare creates a new open scope in which variables can be declared. The variables are visible for the entire rest of the session.

Most operations on free variables block until the variables have been bound (but not Show as used above).

Assignment to dataflow variables is also called unification. It is actually a symmetric operation, e.g. the following binds B to 3:

declare
  A = 3
  B
in
  A = B
 {Show B}

However, variables can only be introduced at the left side of the = operator. So this is a syntax error:

declare
  A = 3
  A = B  %% Error: variable B not introduced
in
 {Show B}

It is possible to introduce multiple variables in a single statement:

declare
   [A B C D] = [1 2 3 4]  %% unification of two lists

In a module definition, toplevel variables can be introduced between the keywords define and end without the need for declare. The range between these two keywords is also their scope. Toplevel variables can optionally be exported.

functor
export Function
define
   ToplevelVariable = 42

   fun {Function}
     42
   end
end

Function and class definitions introduce a new variable with the name of the function/class and assign the new function/class to this variable.

Most Oz statement introduce a new scope and it is possible to introduce local variables at the top of this scope with the in keyword.

fun {Function Arg}
   LocalVar1
in
   LocalVar1 = if Arg == 42 then
		  LocalVar2
	       in
		  LocalVar2 = yes
		  LocalVar2
	       else
		  LocalVar3 = no  %% variables can be initialized when declared
	       in
		  LocalVar3
	       end
   LocalVar1
end

Here, LocalVar1 is visible in the whole body of Function while LocalVar2 is only visible in the then branch and LocalVar3 is only visible in the else branch.

Additionally, new local variables can be introduced everywhere using the keyword local.

if {IsEven 42} then
   {System.showInfo "Here, LocalVar is not visible."}
   local
      LocalVar = "Here, LocalVar IS visible"
   in
      {System.showInfo LocalVar}
   end
end

New variables are also introduced in pattern matching.

case "Rosetta code" of First|_ then {Show First} end %% prints "R"

_ creates a new nameless variable that is initially unbound. It is usually pronounced "don't care".

It is possible to create a read-only view of a variable with the !! operator. This is called a "future". We can wait for such a variable to become bound by another thread and we can read its value, but we can never set it.

declare
  A
  B = !!A %% B is a read-only view of A
in
  thread
     B = 43 %% this blocks until A is known; then it fails because 43 \= 42
  end
  A = 42

Additional operations on variables:

declare
  V = 42
in
  {Wait V}  %% explicitly wait for V to become determined

  if {IsDet V} then  %% check whether V is determined; not recommended
     {Show determined}
  elseif {IsFree V} then  %% check whether V is free; not recommended
     {Show free}
  end

IsFree and IsDet are low-level functions. If you use them, you code is no longer declarative and prone to race conditions when used in a multi-threaded context.

To have mutable references like in imperative languages, use cells:

declare
  A = {NewCell 42}
  OldVal
in
  {Show @A}         %% read a cell with @
  A := 43           %% change its value
  OldVal = A := 44  %% read and write at the same time (atomically)

A is an immutable dataflow variable that is bound to a mutable reference.

PARI/GP

There are two types of local variables, local (mostly deprecated) and my. Variables can be used without declaration or initialization; if not previously used such a variable is a pure variable: technically, a monomial in a variable with name equal to the variable name. This behavior can be forced with the apostrophe operator: regardless of the value (if any) currently stored in x,

'x

displays as (and is treated internally as) x. This is useful when you want to use it as a variable instead of a number (or other type of object). For example,

'x^3+7

is a cubic polynomial, not the number 8, even if x is currently 1.

Pascal

See Delphi

Perl

In perl, variables are global by default and can be manipulated from anywhere in the program. Variables can be used without first being declared, providing that the strict pragmatic directive is not in effect:

sub dofruit {
  $fruit='apple';
}

dofruit; 
print "The fruit is $fruit";

Variables can be declared prior to use and may be prefixed with scope modifiers our, my, or local see scope modifiers for the differences. Variables which haven't been assigned to have the undefined value by default. The undefined value acts just like 0 (if used as a number) or the empty string (if used as a string), except it can be distinguished from either of these with the defined function. If warnings are enabled, perl will print a message like "Use of uninitialized value $foo in addition (+)" whenever you use the undefined value as a number or string.

Initialization and assignment are the same thing in Perl: just use the = operator. Note that the rvalue's context (scalar or list) is determined based on the lvalue.

my $x = @a;                   # Scalar assignment; $x is set to the
                              # number of elements in @a.
my ($x) = @a;                 # List assignment; $x is set to the first
                              # element of @a.
my @b = @a;                   # List assignment; @b becomes the same length
                              # as @a and each element becomes the same.
my ($x, $y, @b) = @a;         # List assignment; $x and $y get the first
                              # two elements of @a, and @b the rest.
my ($x, $y, @b, @c, $z) = @a; # Same thing, and also @c becomes empty
                              # and $z undefined.

The kind of value a variable can hold depends on its sigil, "sigil" being a slang term for "funny character in front of a variable name". $dollarsigns can hold scalars: the undefined value, numbers, strings, or references. @atsigns can hold arrays of scalars, and %percentsigns can hold hashes of scalars (associative arrays mapping strings to scalars); nested data structures are constructed by making arrays or hashes of references to arrays or hashes.

There are two other sigils, but they behave quite unlike the others. A token of the form &foo refers to a subroutine named foo. In older versions of Perl, ampersands were necessary for calling user-defined subroutines, but since they no longer are, they have only a handful of obscure uses, like making references to named subroutines. Note that you can't assign to an ampersand-marked name. But you can assign to a typeglob, a kind of object represented with the notation *var. A typeglob *foo represents the symbol-table entry for all of the otherwise independent variables $foo, @foo, %foo, and &foo. Assigning a string "bar" to *foo makes these variables aliases for $bar, @bar, %bar, and &bar respectively. Alternatively, you can assign a reference to a typeglob, which creates an alias only for the variable of the appropriate type. In particular, you can say *twiddle = sub {...} to change the definition of the subroutine &twiddle without affecting $twiddle and friends.

The strict pragmatic directive

If the strict pragmatic directive is in effect, then variables need explicit scope declaration, so should be prefixed with a my or our keyword depending on the required level of scope:

use strict;
our $fruit;             # declare a variable as global
our $veg = "carrot";    # declare a global variable and define its value

Local and global variables

The following example shows the use of local and global variables:

$fruit="apple";    # this will be global by default

sub dofruit {
  print "My global fruit was $fruit,";    # use the global variable
  my $fruit="banana";                      # declare a new local variable
  print "and the local fruit is $fruit.\n";
}

dofruit;
print "The global fruit is still $fruit";

Phix

Library: Phix/basics

Phix has just five builtin data types:

        <-------- object --------->
        |                |
        +-atom           +-sequence
          |                |
          +-integer        +-string
  • An object can hold any Phix data, specifically either an atom or a sequence.
  • An atom can hold a single floating point numeric value, or an integer.
  • An integer can hold a single whole number (at least +/- 1,000,000,000).
  • A sequence can hold a collection of values, nested to any depth, or a string
  • A string can hold a series of characters, or raw binary data.


Unlike traditional statically typed languages, you do not have to remember dozens of possibly complex data types, and unlike a dynamically typed language you get proper compile-time error messages for simple typos. Phix also allows user defined types, for extra validation and debugging purposes, rather than being fundamentally different to the above. Strings are mutable and the lengths of sequences and strings are only limited by available memory. The sequence is the real powerhouse of Phix, and can be used for lists, tables, trees, etc.

Identifiers may be of any length and are case sensitive. They must start with a letter and then be followed by letters, digits or underscores.

Variables must be declared before use, may be assigned on declaration, and can utilise multiple assignment/sequence decomposition, eg:

integer x = 25, y = 25, z
object {a, b, c} = {{}, 5, "string"}

In the latter statement, c is set to "string", b is set to 5, and a is set to {}. (In multiple assignment, values are assigned right-to-left to avoid having to reorder any subscripts.) You could also use the @= ("all equal") operator to assign a, b and c to the same (entire) thing. Attempts to reference a variable before it has been assigned a value trigger a run-time error, except for object(id) which yields false in that case. Constants are really just variables which must be assigned on declaration and for which subsequent assignment is prohibited.

Variables are by default local and restricted to the current file, routine, or block level. Static file-level variables may also be declared as global to make them visible to other source files. There are no static routine scoped variables, only file-level. Scope resolution is intended to be simple and intuitive, see the link below (Core Language/Declarations/Scope) for more details.

User defined types are declared with a single-parameter function that returns true or false, eg:

type hour(integer x)
    return x >= 0 and x <= 23
end type
hour h1, h2
h1 = 10      -- ok
h2 = 25      -- error! program aborts with a message

Phix has no notion of unsigned numeric types, except via user defined types such as the above which explicitly prevent their use.

You could theoretically write an entire application declaring all variables and parameters as type object, except that it would probably not catch errors the way you might expect it to.

An online copy of the manual can be found at http://phix.x10.mx/docs/html/phix.htm

PHP

<?php
/**
 * @author Elad Yosifon
 */


/*
 * PHP is a weak typed language,
 * no separation between variable declaration, initialization and assignment.
 *
 * variable type is defined by the value that is assigned to it.
 * a variable name must start with a "$" sign (called "sigil", not a dollar sign).
 * variable naming rules:
 *     + case-sensitive.
 *     + first character after $ must not be a number.
 *     + after the first character all alphanumeric chars and  _(underscore) sign is allowed, e.g. $i_am_a_new_var_with_the_number_0
 *
 */

# NULL typed variable
$null = NULL;				var_dump($null);	// output: null

# defining a boolean
$boolean = true;			var_dump($boolean);	// output: boolean true
$boolean = false;			var_dump($boolean);	// output: boolean false

/*
 * casting is made using (TYPE) as a prefix
 */
# bool and boolean is the same
$boolean = (bool)1;			var_dump($boolean);	// output: boolean true
$boolean = (boolean)1;			var_dump($boolean);	// output: boolean true

$boolean = (bool)0;			var_dump($boolean);	// output: boolean false
$boolean = (boolean)0;			var_dump($boolean);	// output: boolean false

# defining an integer
$int = 0;				var_dump($int);		// output: int 0

# defining a float,
$float = 0.01;				var_dump($float);	// output: float 0.01

# which is also identical to "real" and "double"
var_dump((double)$float);					// output: float 0.01
var_dump((real)$float);						// output: float 0.01

# casting back to int (auto flooring the value)
var_dump((int)$float);						// output: int 0
var_dump((int)($float+1));					// output: int 1
var_dump((int)($float+1.9));					// output: int 1

# defining a string
$string = 'string';
var_dump($string);						// output: string 'string' (length=6)

# referencing a variable (there are no pointers in PHP).
$another_string = &$string;
var_dump($another_string);
								// output: string 'string' (length=6)

$string = "I'm the same string!";
var_dump($another_string);
								// output: string 'I'm the same string!' (length=20)
# "deleting" a variable from memory
unset($another_string);

$string = 'string';
/*
 * a string can also be defined with double-quotes, HEREDOC and NOWDOC operators.
 * content inside double-quotes is being parsed before assignment.
 * concatenation operator is .=
 */
$parsed_string = "This is a $string";
var_dump($parsed_string);
								// output: string 'This is a string' (length=16)
$parsed_string .= " with another {$string}";
var_dump($parsed_string);
								// output: string 'This is a string with another string' (length=36)

# with string parsing
$heredoc = <<<HEREDOC
This is the content of \$string: {$string}
HEREDOC;
var_dump($heredoc);
								// output: string 'This is the content of $string: string' (length=38)

# without string parsing (notice the single quotes surrounding NOWDOC)
$nowdoc = <<<'NOWDOC'
This is the content of \$string: {$string}
NOWDOC;
var_dump($nowdoc);
								// output: string 'This is the content of \$string: {$string}' (length=42)

# as of PHP5, defining an object typed stdClass => standard class
$stdObject = new stdClass();	var_dump($stdObject);
								// output: object(stdClass)[1]
# defining an object typed Foo
class Foo {}
$foo = new Foo();		var_dump($foo);
								// output: object(Foo)[2]
# defining an empty array
$array = array();		var_dump($array);
								// output: array {empty}

/*
 * an array with non-integer key is also considered as an associative array(i.e. hash table)
 * can contain mixed variable types, can contain integer based keys and non-integer keys
 */
$assoc = array(
	0 => $int,
	'integer' => $int,
	1 => $float,
	'float' => $float,
	2 => $string,
	'string' => $string,
	3 => NULL, // <=== key 3 is NULL
	3, // <=== this is a value, not a key (key is 4)
	5 => $stdObject,
	'Foo' => $foo,
);
var_dump($assoc);

// output:
// =======
//	array
//	    0 => int 0
//	    'integer' => int 0
//	    1 => float 0.01
//	    'float' => float 0.01
//	    2 => string 'string' (length=6)
//	    'string' => string 'string' (length=6)
//	    3 => null
//	    4 => int 3
//	    5 =>
//	    	object(stdClass)[1]
//	    'Foo' =>
//		    object(Foo)[2]



/*
 * all variables are "global" but not reachable inside functions(unless specifically "globalized" inside)
 */

function a_function()
{
	# not reachable
	var_dump(isset($foo));				// output: boolean false

	global $foo;
	# "global" (reachable) inside a_function()'s scope
	var_dump(isset($foo));				// output: boolean true
}

a_function();

/**
 * there is another special type of variable called (Resource).
 * for more info regarding Resources:
 * @url http://php.net/manual/en/language.types.resource.php
 * @url http://php.net/manual/en/resource.php
 */

PicoLisp

You can control the local bindings of symbols with functions like 'use' or 'let':

(use (A B C)
   (setq A 1  B 2  C 3)
   ... )

This is equivalent to

(let (A 1  B 2  C 3)
   ... )

The parentheses can be omitted if there is only a single variable

(use A
   (setq A ..)
   ... )

(let A 1
   ...)

Other functions that handle local bindings are 'let?', 'bind', 'job', 'with' or 'for'.

PL/I

/* The PROCEDURE block and BEGIN block are used to delimit scopes. */

declare i float; /* external, global variable, excluded from the */
                 /* local area (BEGIN block) below.              */
begin;
   declare (i, j) fixed binary; /* local variable */
   get list (i, j);
   put list (i,j);
end;

/* Examples of initialization. */

declare p fixed initial (25);
declare q(7) fixed initial (9, 3, 5, 1, 2, 8, 15);
   /* sets all elements of array Q at run time, on block entry. */
declare r(7) fixed initial (9, 3, 5, 1, 2, 8, 15);
   /* sets all elements of array R at compile time. */

p = 44; /* run-time assignment. */
q = 0; /* run-time initialization of all elements of Q to zero. */
q = r; /* run-time assignment of all elements of array R to     */
       /* corresponding elemets of S.                           */

Pony

var counter: I32 = 10 // mutable variable 'counter' with value 10
let temp: F64 = 36.6  // immutable variable 'temp'
let str: String       // immutable variable 'str' with no initial value
str = "i am a string" // variables must be initialized before use
let another_str = "i am another string" // type of variable 'another_str' infered from assigned value

let b  = true
let b' = false // variable names can contain ' to make a distinct variable with almost the same name

Destructive read

var x: I32 = 10
var y: I32 = x = 20
try
  Fact(x == 20) // x gets the new value of 20
  Fact(y == 10) // y gets the old value of x which is 10
end

PowerShell

Variables in PowerShell start with a $ character, they are created on assignment and thus don't need to be declared:

$s = "abc"
$i = 123

Uninitialized variables expand to nothing. This may be interpreted for example as an empty string or 0, depending on context:

4 + $foo              # yields 4
"abc" + $foo + "def"  # yields "abcdef"

Variables all show up in the Variable: drive and can be queried from there with the usual facilities:

Get-ChildItem Variable:

Since Variables are provided via a flat filesystem, they can be manipulated using the common cmdlets for doing so. For example to delete a variable one can use

Remove-Item Variable:foo

as if it were a file or a registry key. There are, however, several cmdlets dealing specifically with variables:

Get-Variable      # retrieves the value of a variable
New-Variable      # creates a new variable
Set-Variable      # sets the value of a variable
Clear-Variable    # deletes the value of a variable, but not the variable itself
Remove-Variable   # deletes a variable completely

Prolog

Variables in imperative languages are, in essence, named memory locations where items of data can be stored. Prolog, as a declarative language, has no variables in this sense, and therefore has no way of declaring them or assigning to them. Prolog variables are more like variables in formal logic or in mathematics. Here is a very simple Prolog program:

mortal(X) :- man(X).
man(socrates).

The symbol :- may be read as 'when' or 'if', so that the first line is equivalent to the statement in predicate logic ∀x: pxqx where px is interpreted as 'x is a man' and qx is interpreted as 'x is mortal'. Prolog notation, however, requires variable names to start with (or consist of) a capital letter: this is how the interpreter knows that socrates is not a variable.

We can of course use more than one variable:

student(X,Y) :- taught(Y,X).
taught(socrates,plato).

When we put queries to the Prolog system, it will seek to find a consistent set of interpretations that allows our query to be true in the light of the facts and rules we have provided:

?- mortal(socrates).
yes
?- student(X,socrates).
X=plato
?- student(socrates,X).
no

And so forth. We can reuse the same variable names in different statements as much as we like, but within each statement the same variable must be capable of referring to the same term.

?- mortal(zeus).
no

Prolog does not answer like that because it is a connoisseur of the mythology; and in any case several ancient writers report that Zeus's tomb could be seen on Crete. But the rule states that X is mortal if X—the same x—is a man, so it could only unify successfully if zeus and socrates were the same (which even his most devoted admirers did not claim). If our original rule had said

mortal(X) :- man(Y).

then the two variables would be able to refer to two different terms, and the Prolog interpreter would agree that mortal(zeus).

PureBasic

; Variables are initialized when they appear in sourcecode with default value of 0 and type int
Debug a
; or value "" for a string, they are not case sensitive
Debug b$
; This initializes a double precision float, if type is following the dot
Debug c.d
; They can be initialized with define (double precision float, string, integer)
Define d.d = 3.5, e$ = "Test", f.i = a + 2
; Define can have a default type (all bytes except j which is long):
Define.b g, h, j.l
; Define without following variables sets default type. In this case to single precision float
Define.f
; So this will be an single precision float and no integer
Debug k
; EnableExplicit forces declaration of used variables with define
EnableExplicit
; Will throw an error because L isn't initialized
Debug L
DisableExplicit
; Global Variables are available in Procedures and Threads too
Global M = 3, N = 2
Procedure Dummy(parameter1, parameter2 = 20)
  ; Parameter contain values which where used when calling the function,
  ; their types have to be specified in the above Procedure header.
  ; The last ones can have default values which get applied if this parameter is not given.

  ; Variables in Procedures are separate from those outside,
  ; so d can be initialized again with another type
  ; which would otherwise lead to an error
  d.i
  ; Protected makes a variable local even if another one with same name is declared as global (see above)
  Protected M = 2
  ; Shares a variable with main program like it was declared by global
  Shared a
  ; prevents a variable to be initialized with default value again when procedure is called a second time,
  ; could be used for example as a counter, which contains the number of times a function was called
  Static a
  ; N here also would have a value of 2, while for example
  ; f would, when named, initialize a new variable, and so have a value of 0
EndProcedure
; finally there are constants which are prefixed by an #:
#Test = 1
; Their value cannot be changed while program is running
#String_Constant = "blubb"
; In constrast  to variables, a constant has no types except an (optional) $ sign to mark  it as string constant
#Float_Constant = 2.3
; Maps, LinkedLists , Arrays and Structures are not handled here, because they are no elemental variables

Python

Names in Python are not typed, although all the objects referred to by them, are. Names are lexically scoped by function/method/class definitions, and must be defined before use.

Names in global statements are looked up in the outermost context of the program or module. Names in a nonlocal statement are looked up in the order of closest enclosing scope outwards.

# these examples, respectively, refer to integer, float, boolean, and string objects
example1 = 3
example2 = 3.0
example3 = True
example4 = "hello"

# example1 now refers to a string object.
example1 = "goodbye"

Quackery

Quackery does not have variables.

It is a stack based language, and data items (numbers, names, and nests — i.e. bigints, executable tokens, and dynamic arrays of numbers, names and nests) reside on the Quackery data stack ("the stack") during program execution.

Additional data stacks, referred to as ancillary stacks, provide the functionality of global and scoped variables. There are several predefined ancillary stacks, for example base, which stores the current base for numerical i/o, and temp, which is available for use as a local variable. A new named ancillary stack can be defined thus:

[ stack ] is mystack

The following Quackery shell dialogue demonstrates the use of several words relating to ancillary stacks. (ancillary-stack-name copy echo allows us to see the contents of an ancillary stack.)

Welcome to Quackery.

Enter "leave" to leave the shell.

/O> [ stack ] is A
... [ stack ] is B
... A copy echo cr
... 42 A put
... ' words A put
... ' [ 23 [ over swap ] 801 ] A put
... A copy echo cr
... A take echo cr
... A take echo cr
... A copy echo cr
... 
[ stack ]
[ stack 42 words [ 23 [ over swap ] 801 ] ]
[ 23 [ over swap ] 801 ]
words
[ stack 42 ]

Stack empty.

/O> A share echo cr
... A copy echo cr
... 
42
[ stack 42 ]

Stack empty.

/O> 23 A replace
... A copy echo 
... 
[ stack 23 ]
Stack empty.

/O> -1 A tally
... A copy echo
... 
[ stack 22 ]
Stack empty.

/O> A B move
... A copy echo cr
... B copy echo cr
... B release
... B copy echo
... 
[ stack ]
[ stack 22 ]
[ stack ]
Stack empty.

R

Variables are dynamically typed, so they do not need to be declared and instantiated separately. <- and = are both used as the assignment operator, though <- is preferred, for compatibility with S-Plus code.

foo <- 3.4
bar = "abc"

It is possible to assign multiple variables with the same value, and to assign values from left to right.

baz <- quux <- 1:10
TRUE -> quuux

There are also global assignment operators, <<- and ->>. From their help page:

The operators '<<-' and '->>' cause a search to made through the
environment for an existing definition of the variable being
assigned.  If such a variable is found (and its binding is not
locked) then its value is redefined, otherwise assignment takes
place in the global environment.

In practice, this usually means that variables are assigned in the user workspace (global environment) rather than a function.

a <- 3

assignmentdemo <- function()
{
   message("assign 'a' locally, i.e. within the scope of the function")
   a <- 5
   message(paste("inside assignmentdemo, a = ", a))
   message(paste("in the global environment, a = ", get("a", envir=globalenv())))
   
   message("assign 'a' globally")
   a <<- 7
   message(paste("inside assignmentdemo, a = ", a))
   message(paste("in the global environment, a = ", get("a", envir=globalenv())))
}
assignmentdemo()
assign 'a' locally, i.e. within the scope of the function
inside assignmentdemo, a =  5
in the global environment, a =  3
assign 'a' globally
inside assignmentdemo, a =  5
in the global environment, a =  7

Finally, there is also the assign function, where you choose the environment to assign the variable.

assign("b", TRUE)                         #equivalent to b <- TRUE
assign("c", runif(10), envir=globalenv()) #equivalent to c <<- runif(10)

Racket

#lang racket

;; define a variable and initialize it
(define foo 0)
;; increment it
(set! foo (add1 foo))

;; Racket is lexically scoped, which makes local variables work:
(define (bar)
  (define foo 100)
  (set! foo (add1 foo))
  foo)
(bar) ; -> 101

;; and it also makes it possible to have variables with a global value
;; that are accessible only in a specific local lexical scope:
(define baz
  (let () ; used to create a new scope
    (define foo 0)
    (define (bar)
      (set! foo (add1 foo))
      foo)
    bar)) ; this is the value that gets bound to `baz'
(list (baz) (baz) (baz)) ; -> '(1 2 3)

;; define a new type, and initialize a variable with an instance
(struct pos (x y))
(define p (pos 1 2))
(list (pos-x p) (pos-y p)) ; -> '(1 2)

;; for a mutable reference, a struct (or some specific fields in a
;; struct) can be declared mutable
(struct mpos (x y) #:mutable)
(define mp (mpos 1 2))
(set-mpos-x! mp 11)
(set-mpos-y! mp 22)
(list (mpos-x mp) (mpos-y mp)) ; -> '(11 22)

;; but for just a mutable value, we have boxes as a builtin type
(define b (box 10))
(set-box! b (add1 (unbox b)))
(unbox b) ; -> 11

;; (Racket has many more related features: static typing in typed
;; racket, structs that are extensions of other structs,
;; pattern-matching on structs, classes, and much more)

Raku

(formerly Perl 6) Much of what is true for Perl 5 is also true for Raku. Some exceptions:

There are no typeglobs in Raku.

Assigning an array to a scalar variable now makes that scalar variable a reference to the array:

my @y = <A B C D>; # Array of strings 'A', 'B', 'C', and 'D'
say @y[2]; # the @-sigil requires the container to implement the role Positional
@y[1,2] = 'x','y'; # that's where subscripts and many other things come from
say @y; # OUTPUT«[A x y D]␤» # we start to count at 0 btw.

my $x = @y; # $x is now a reference for the array @y

say $x[1]; # prints 'x' followed by a newline character

my Int $with-type-check; # type checks are enforced by the compiler

my Int:D $defined-i = 10; # definedness can also be asked for and default values are required in that case

my Int:D $after-midnight where * > 24 = 25; # SQL is fun and so is Raku

my \bad = 'good'; # if you don't like sigils
say bad; # you don't have to use them
say "this is quite bad"; # but then string interpolation
say "this is quite {bad}" # becomes more wordy

Laziness is a big topic in Raku. Sometimes Raku programmers are so lazy, they can't even be bothered with giving variables names.

say ++$; # this is an anonymous state variable
say ++$; # this is a different anonymous state variable, prefix:<++> forces it into numerical context and defaults it to 0
say $+=2 for 1..10; # here we do something useful with another anonymous variable

sub foo { $^a * $^b } # for positional arguments we often can't be bothered to declare them or to give them fancy names
say foo 3, 4;
Output:
1
1
2
4
6
12

(Includes code modified from http://design.raku.org/S02.html#Built-In_Data_Types. See this reference for more details.)

Rascal

The effect of a variable declaration is to introduce a new variable Name and to assign the value of expression Exp to Name. A variable declaration has the form

 Type Name = Exp;

A mention of Name later on in the same scope will be replaced by this value, provided that Name’s value has not been changed by an intermediate assignment. When a variable is declared, it has as scope the nearest enclosing block, or the module when declared at the module level.

There are two rules you have to take into account. Double declarations in the same scope are not allowed. Additionally, the type of Exp should be compatible with Type, i.e., it should be a subtype of Type.

As a convenience, also declarations without an initialization expression are permitted inside functions (but not at the module level) and have the form

Type Name;

and only introduce the variable Name.

Rascal provides local type inference, which allows the implicit declaration of variables that are used locally in functions. There are four rules that apply when doing so. (1) An implicitly declared variable is declared at the level of the current scope, this may the whole function body or a block nested in it. (2) An implicitly declared variable gets as type the type of the first value that is assignment to it. (3) If a variable is implicitly declared in different execution path of a function, all these implicit declarations should result in the same type. (4) All uses of an implicitly declared variable must be compatible with its implicit type.

Examples

Two explicit variable declarations:

rascal>int max = 100;
int: 100
rascal>min = 0;
int: 0

An implicit variable declaration

rascal>day = {<"mon", 1>, <"tue", 2>, <"wed",3>, 
>>>>>>>       <"thu", 4>, <"fri", 5>, <"sat",6>, <"sun",7>};
rel[str, int]: {
  <"thu",4>,
  <"mon",1>,
  <"sat",6>,
  <"wed",3>,
  <"tue",2>,
  <"fri",5>,
  <"sun",7>
}

Variable declaration and assignment leading to type error

rascal>int month = 12;
int: 12
rascal>month ="December";
|stdin:///|(7,10,<1,7>,<1,17>): Expected int, but got str

Pitfalls Local type inference for variables always uses the smallest possibe scope for a variable; this implies that a variable introduced in an inner scope is not available outside that scope. Here is how things can go wrong:

rascal>if( 4 > 3){ x = "abc"; } else { x = "def";}
str: "abc"
rascal>x;
|stdin:///|(0,1,<1,0>,<1,1>): Undeclared variable, function or constructor: x

REXX

assignments via =

REXX has only one type of variable:   a (character) string.


There is no need to declare anything   (variables, entry points, subroutines, functions, etc)   (indeed, there isn't any way to declare anything at all).

All unassigned REXX variables have a default value of the variable name (in uppercase).

There isn't any way to initialize a variable except to assign it a value   (by one of the methods below), however,
there is a way to "initialize" a (stemmed) array in REXX with a default value) ── see the 6th section below,
default value for an array.

To assign some data (value) to a variable, one method is to use the assignment operator, an equal sign   (=):

aa = 10                                /*assigns chars    10  ───►  AA  */
bb = ''                                /*assigns a null value ───►  BB  */
cc = 2*10                              /*assigns chars    20  ───►  CC  */
dd = 'Adam'                            /*assigns chars Adam   ───►  DD  */
ee = "Adam"                            /*same as above        ───►  EE  */
ff = 10.                               /*assigns chars   10.  ───►  FF  */
gg = '10.'                             /*same as above        ───►  GG  */
hh = "+10"                             /*assigns chars  +10   ───►  hh  */
ii = 1e1                               /*assigns chars  1e1   ───►  ii  */
jj = +.1e+2                            /*assigns chars  .1e+2 ───►  jj  */

Variables   aa,   ff,   gg,   hh,   ii,   and  jj   will all be considered numerically equal in REXX   (but not exactly equal except for   ff   and   gg).

assignments via PARSE

kk = '123'x                            /*assigns hex  00000123 ───► KK  */
kk = 'dead beaf'X                      /*assigns hex  deadbeaf ───► KK  */
ll = '0000 0010'b                      /*assigns blank ───► LL  (ASCII) */
mm = '0000 0100'B                      /*assigns blank ───► MM  (EBCDIC)*/

xxx = '11 2. 333 -5'
parse var xxx   nn oo pp qq rr
                                       /*assigns     11    ───►  NN     */
                                       /*assigns     2.    ───►  OO     */
                                       /*assigns    333    ───►  PP     */
                                       /*assigns     ─5    ───►  QQ     */
                                       /*assigns   "null"  ───►  RR     */

                           /*a  "null"  is a string of length zero (0), */
                           /*and is not to be confused with a null char.*/

cat = 'A cat is a lion in a jungle of small bushes.'
                                       /*assigns a literal ───►  CAT    */

assignments via VALUE

Assignments can be made via the   value   BIF   [Built-In Function]   (which also has other capabilities), the
capability used here is to create a variable name programmatically (normally using concatenation or abuttal).

call value 'CAT', "When the cat's away, the mice will play."  
                                       /*assigns a literal ───►  CAT    */
yyy='CA'
call value yyy'T', "Honest as the Cat when the meat's out of reach."
                                       /*assigns a literal ───►  CAT    */
yyy = 'CA'
call value yyy || 'T', "Honest as the Cat when the meat's out of reach."
                                       /*assigns a literal ───►  CAT    */

unassigned variables

There are methods to catch unassigned variables in REXX.

/*REXX pgm to do a "bad" assignment  (with an unassigned REXX variable).*/

signal on noValue                      /*usually, placed at pgm start.  */

xxx=aaaaa                              /*tries to assign aaaaa ───► xxx */

say xxx 'or somesuch'
exit

noValue:                               /*this can be dressed up better. */
badLine  =sigl                         /*REXX line number that failed.  */
badSource=sourceline(badLine)          /*REXX source line ···           */
badVar   =condition('D')               /*REXX var name that's ¬ defined.*/
say
say '*** error! ***'
say 'undefined variable' badvar "at REXX line number" badLine
say
say badSource
say
exit 13

Note:   the value (result) of the   condition   BIF can vary in different implementations of a REXX interpreter.

output using Regina (various versions), PC/REXX, Personal REXX, and ooRexx.

*** error! ***
undefined variable AAAAA at REXX line number 5

xxx=aaaaa                              /*tries to assign aaaaa ───► xxx */

output using R4 REXX:

Error 46 : Invalid variable reference (NOVALUE)
Information: Variable aaaaa does not have an assigned value
Error occurred in statement# 5
Statement source: xxx= aaaaa
Statement context: D:\variabl4.rex, procedure: variabl4

scope of variables

REXX subroutines/functions/routines/procedures can have their own "local" variables if the   procedure   statement is used.
Variables can be shared with the main program if the variables are named on the procedure statement with the   expose   keyword.

/*REXX pgm shows different scopes of a variable: "global"  and  "local".*/
q = 55          ;    say ' 1st q='  q  /*assign a value ───►  "main"  Q.*/
call sub        ;    say ' 2nd q='  q  /*call a procedure subroutine.   */
call gyro       ;    say ' 3rd q='  q  /*call a procedure with EXPOSE.  */
call sand       ;    say ' 4th q='  q  /*call a subroutine or function. */
exit                                   /*stick a fork in it, we're done.*/
/*──────────────────────────────────SUB subroutine──────────────────────*/
sub:  procedure                        /*use PROCEDURE to use local vars*/
q = -777        ;    say ' sub q='  q  /*assign a value ───► "local" Q. */
return
/*──────────────────────────────────GYRO subroutine─────────────────────*/
gyro: procedure expose q               /*use EXPOSE to use global var Q.*/
q = "yuppers"   ;    say 'gyro q='  q  /*assign a value ───► "exposed" Q*/
return
/*──────────────────────────────────SAND subroutine─────────────────────*/
sand:                                  /*all REXX variables are exposed.*/
q = "Monty"     ;    say 'sand q='  q  /*assign a value ───► "global"  Q*/
return

output

 1st q= 55
 sub q= -777
 2nd q= 55
gyro q= yuppers
 3rd q= yuppers
sand q= Monty
 4th q= Monty

Programming note:   there is also a method in REXX to expose a list of variables.

default value for an array

There is a way in REXX to assign a default value (or an initial value, if you will) to a (stemmed) array.

aaa. = '───────nope.'                  /*assign this string as a default*/
aaa.1=1                                /*assign   1   to first  element.*/
aaa.4=4.                               /*   "     4    " fourth    "    */
aaa.7='lucky'                          /*   "     7    " seventh   "    */
              do j=0  to 8             /*go through a bunch of elements.*/
              say 'aaa.'||j '=' aaa.j  /*display element # and its value*/
              end                      /*we could've started J at  -100.*/

dropping a variable

In REXX, dropping a variable (this can be thought of as deallocating it or setting the value back to its "default").


Note that the storage used by the variable's (old) value is not truly deallocated, but its storage is returned to the
pool of storage available for allocation of other REXX variables (and their values).   This action isn't mandatory
for the REXX language   (or for that matter, not even specified),   but it's apparently what all   (Classic) REXX
interpreters do at the the time of this writing.

radius=6.28                            /*assign a value to a variable.  */
                say 'radius =' radius
drop radius                            /*now, "undefine" the variable.  */
                say 'radius =' radius

Note:   The value of an undefined (or deallocated) REXX variable is the uppercased name of the REXX variable name.

output

radius = 6.28
radius = RADIUS

compound variables

In additions to the (simple) variables described above, REXX defines compound variables that consist of a stem (symbol.) followed by a list of period-separated simple variables and constants The compound variable's tail is computed by concatenating the variables' values (which can be any string) with the constants and the intervening periods.

var.='something' /* sets all possible compound variables of stem var. */
x='3 '
var.x.x.4='something else'
Do i=1 To 5
  a=left(i,2)
  Say i var.a.a.4 "(tail is '"a||'.'||a||'.'||'4'"')"
  End

Output:

1 something (tail is '1 .1 .4')
2 something (tail is '2 .2 .4')
3 something else (tail is '3 .3 .4')
4 something (tail is '4 .4 .4')
5 something (tail is '5 .5 .4')

Ring

To create a new variable, you just need to determine the variable name & value. The value will determine the variable type and you can change the value to switch between the types using the same variable name.

Syntax:

<Variable Name> = <Value>

The operator ‘=’ is used here as an Assignment operator and the same operator can be used in conditions, but for testing equality of expressions.

The Variable will contains the real value (not a reference). This means that once you change the variable value, the old value will be removed from memory (even if the variable contains a list or object).

Ring is a dynamic programming language that uses Dynamic Typing.

x = "Hello"             # x is a string
see x + nl
x = 5                   # x is a number (int)
see x + nl
x = 1.2                 # x is a number (double)
see x + nl
x = [1,2,3,4]           # x is a list
see x                   # print list items
x = date()              # x is a string contains date
see x + nl
x = time()              # x is a string contains time
see x + nl
x = true                # x is a number (logical value = 1)
see x + nl
x = false               # x is a number (logical value = 0)
see x + nl

We can use the assignment operator ‘=’ to copy variables. We can do that to copy values like strings & numbers. Also, we can copy complete lists & objects. The assignment operator will do a complete duplication for us. This operation called Deep Copy

list = [1,2,3,"four","five"]
list2 = list
list = []
See list        # print the first list - no items to print
See "********" + nl
See list2       # print the second list - contains 5 items

Ring is a weakly typed language, this means that the language can automatically convert between data types (like string & numbers) when that conversion make sense.

Rules:

<NUMBER> + <STRING> --> <NUMBER>
<STRING> + <NUMBER> --> <STRING>

The same operator ‘+’ can be used as an arithmetic operator or for string concatenation.

x = 10                  # x is a number
y = "20"                # y is a string
sum = x + y             # sum is a number (y will be converted to a number)
Msg = "Sum = " + sum    # Msg is a string (sum will be converted to a string)
see Msg + nl

RPL

A global variable is declared with the STO instruction after the variable name. A local variable, which disappears at end of execution, is declared with the instruction before the variable name, which is idiomatically in lowercase. in both cases, the variable is initialized with the value at stack level 1, whatever its type.

123 'V' STO
123 → v

A new value can then be assigned with the STO instruction:

"AZ" 'V' STO
"AZ" 'v' STO

A variable can contain any type of data, which can change without need for reinitialization, as shown above. It is possible to add, substract, multiple, divide... the content of stack level 1 to a variable with specific instructions, resp. STO+, STO-, STO*, STO/... If the variable contains a list, STO+ will append or prepend the content of stack level 1, according to the order of the arguments:

456 'MYLIST' STO+     @ prepend
'MYLIST' 456 STO+     @ append

STO- and STO/ have the same versatility:

123 'V' STO-          @ V ← 123 - V
'V' 123 STO-          @ V ← V - 123

Ruby

Information taken from Variables page at the Ruby User's Guide

Ruby has three kinds of variables, one kind of constant and exactly two pseudo-variables. The variables and the constants have no type. While untyped variables have some drawbacks, they have many more advantages and fit well with ruby's quick and easy philosophy.

Variables must be declared in most languages in order to specify their type, modifiability (i.e., whether they are constants), and scope; since type is not an issue, and the rest is evident from the variable name as you are about to see, we do not need variable declarations in ruby.

The first character of an identifier categorizes it at a glance:

$ global variable
@ instance variable
[a-z] or _ local variable
[A-Z] constant

The only exceptions to the above are ruby's pseudo-variables: self, which always refers to the currently executing object, and nil, which is the meaningless value assigned to uninitialized variables. Both are named as if they are local variables, but self is a global variable maintained by the interpreter, and nil is really a constant. As these are the only two exceptions, they don't confuse things too much.

Referencing an undefined global or instance variable returns nil. Referencing an undefined local variable throws a NameError exception.

$a_global_var = 5
class Demo
  @@a_class_var = 6
  A_CONSTANT = 8
  def initialize
    @an_instance_var = 7
  end
  def incr(a_local_var)
    @an_instance_var += a_local_var
  end
end

Rust

In Rust a variable needs to be declared before it is used. A variable is declared using the "let" command. Some common data types are: i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, bool, char and String. Rust is strongly typed and strictly typed, however the Rust compiler can infer the data types based on how the variable is used. Therefore there are several different options for variable declaration:

let var01; // The compiler will infer the type or the compiler will ask for type annotations if the type can't be inferred
let var02: u32; // The compiler will check if a value is assigned to var02 before being used 
let var03 = 5; // The compiler will infer the type or it will fallback to i32
let var04 = 5u8; // Initialization to unsigned 8 bit (u8) number 5
let var05: i8 = 5; // Type annotation + Initialization
let var06: u8 = 5u8; // Type annotation + Initialization
var01 = var05; // This line makes the compiler infer var01 should be a signed 8 bit number (i8).
var02 = 9u32; // Assigning to var02 after declaration

Variables are immutable by default. So all the above declarations will all create an immutable variable that can't be reassigned once assigned for the first time. To make the variable mutable you need to add the "mut" keyword like in the following examples:

let mut var07;
let mut var08: f32;
let mut var09 = 1.5;
let mut var10 = 2.5f32;
let mut var11: f32 = 5.0;
let mut var12: f64 = 5.0f64;
var07 = var11; // This line makes the compiler infer var07 should be a f32.
var08 = 3.1416f32; // Assigning to var08 after declaration
var08 = 2.7183f32; // Reassigning var08 since it can be reassigned since it is mutable.

The compiler emits a warning if a variable is not used. To suppress the warning, you need to put an underscore "_" prefix before the variable name:

let _notused = 10;
let mut _mutnotused = 42;

Scala

Variables in Scala can have three different scopes depending on the place where they are being used. They can exist as fields, as method parameters and as local variables. Below are the details about each type of scope:

  • Fields are variables that belong to an object. The fields are accessible from inside every method in the object. Fields can also be accessible outside the object depending on what access modifiers the field is declared with. Object fields can be both mutable or immutable types and can be defined using respectively var or val.
  • Method parameters are variables, which are used to pass the value inside a method when the method is called. Method parameters are only accessible from inside the method but the objects passed in may be accessible from the outside, if you have a reference to the object from outside the method. Method parameters are always immutable and defined by val keyword. In algebraic datatypes (Scala's case classes), -think records- the parameters looks like method parameters. In this case they are fields.
  • Local variables are variables declared inside a method. Local variables are only accessible from inside the method, but the objects you create may escape the method if you return them from the method. Local variables can be both mutable or immutable types and can be defined using respectively var or val.

Seed7

Seed7 variables must be defined with type and initialization value, before they are used. There are global variables and variables declared local to a function.

$ include "seed7_05.s7i";

var integer: foo is 5;   # foo is global

const proc: aFunc is func
  local
    var integer: bar is 10;   # bar is local to aFunc
  begin
    writeln("foo + bar = " <& foo + bar);
  end func;

const proc: main is func
  begin
    aFunc;
  end func;

Set lang

There is no declared intialization of variables, or datatypes. Set already initializes 52 variables, named a-z with a value of 0 (ASCII for Null) each, and A-Z with ASCII values corresponding to their characters (65-90). There is also a question mark variable, which represents the number of the current line of code being executed.

set a 0        > Useless intialization - All lowercase variables have an initial value of 0
set b 66       > Simple variable assignment - ''b'' is now the ASCII value of 66, or the character 'B'
[c=0] set c 5  > Conditional variable assignment - If ''c'' is 0, then set ''c'' to 5
set ? 6        > A "goto" command; Setting the ''?'' variable defines the line of code to be executed next
set z 1        > This line of code will never be executed, because the previous skipped it
set c 10       > Line #5 skipped to here
set d A        > Assigning a variable to another variable - ''d'' is now ASCII 65, or the character 'A'
set b !        > The ''!'' deals with I/O. Setting a variable to it receives an input character and assigns it to the variable
set ! a        > Setting the exclamation point to a variable outputs that variable
set e (d+1)    > Combiners are defined inside round brackets - () - and have an addition and a subtraction function
set f (e-1)    > Variable ''e'' was assigned to ''d'' + 1 (65 + 1 = 66, character B), and ''f'' was assigned to ''e'' - 1 (66 - 1 = 65, character A)

smart BASIC

Data Types

There are two variable data types in smart BASIC; numeric and string.

Numeric variables can contain real and complex numbers.

x = 14
y = 0.4E3
z = 3-2i

String variables use the dollar symbol ($) at the end.

t$ = "Name"

String values may include the quotation symbol " by specifying double quotes "".

n$ = """This is a quoted text"""

Initialization

Variable names are NOT case sensitive.

Variable names may not begin with a number.

Variables are initialized when assigned as either numeric or strings. However, smart BASIC in unique in that it will automatically interpret a numeric value from a string if utilized in that manner.

n = 4
a$ = "6 feet"
PRINT n * a$
Output:
24

Declaration

Variables do not require declaration in smart BASIC.

Only variable arrays need to be declared by size if the array is over 10 elements.

Arrays are DIM-ensioned by default with a size of 10. Larger arrays must be pre-DIM-entioned.

DIM name$(100)

Assignment

The command LET used in standard BASIC may be omitted.

LET x = 1 may be shortened to just x = 1.

Precision

Numeric variable precision is limited to 64-bit representation with 1 bit for sign, 11 bits for power, and 52 bits for mantissa. Any integer greater than 2^52 (4,503,599,627,370,496) will be approximated.

Scope

All variables are local in smart BASIC. Variables within the main program are localized from variables within defined functions as well as other defined functions. However, variables may be accessed outside of their localized areas by preceding the variable with the function name and a period. By extension, the main program scope is null, therefore non-function variables may be accessed within functions with a period before the variable and a null scope (for example: .x).

DEF MyFunction(x)
MyF2 = x^2
MyF3 = x^3
MyFunction = MyF2 + MyF3
END DEF

To access the variables within the function:

x = MyFunction(3)
PRINT x; MyFunction.MyF2; MyFunction.MyF3

Variables may also be made global using the DEF command:

DEF num=123

SNOBOL4

Local variables in Snobol are declared in a function definition prototype string:

        define('foo(x,y)a,b,c') :(foo_end)
foo     a = 1; b = 2; c = 3
        foo = a * ( x * x ) + b * y + c :(return)
foo_end

This defines a function foo( ) taking two arguments x,y and three localized variables a,b,c. Both the argument parameters and vars are dynamically scoped to the function body, and visible to any called functions within that scope. The function name also behaves as a local variable, and may be assigned to as the return value of the function. Any variable initialization or assignment is done explicitly within the function body. Unassigned variables have a null string value, which behaves as zero in numeric context.

Snobol does not support static or lexical scoping, or module level namespaces. Any variables not defined in a prototype are global to the program.

SPL

Variable declaration. In SPL variables do not need and do not have declaration.

Initialization. In SPL variables are autodeclared according to their usage. For example, this one-line program is valid because it is evident what is expected:

a += 1

In contrast, this one-line program raises an error because it is not evident what object "a" is:

a = a+1

Assignment.

a = 1
b,c,d = 0

Datatypes. In SPL an object can be: number, text, stack, array, group, function.

Scope. There is a main program body scope and individual function scopes. Variable from any scope can be accessed from any other scope by specifying the desired scope and variable name like "scope.variable". Variable from main program body scope can be accessed from any other scope like ".variable", because main program body scope is an empty space.

Referencing. An object can become a reference to another object using "~" symbol, for example:

r = ~a
a = 3
#.output(r)
r = 5
#.output(a)
Output:
3
5

SSEM

A variable is simply a storage address that the programmer chooses to treat as a variable. It requires no special allocation, declaration, etc., and an initial value can be loaded into it just as with any other storage address. Naturally, there is no concept of scope: any variable can be accessed and modified from any part of the program. To assign a fresh value to a variable, first form the desired number in the accumulator and then use a 110 c to <address> instruction to store it.

Note that accessing a value from a variable requires several operations, because the 010 -<address> to c instruction (corresponding to load) negates the value in the process of loading it into the accumulator. The negation must thus be written back into storage and another 010 used to load the negation of that, which is the value we want. Depending on the algorithm, it will be found convenient either to overwrite the variable itself with its negation or to use a separate address as a scratch variable (thus preserving the original).

SuperCollider

Variables are always local to the scope of a closure or an object.

// variable declaration
var table, chair;

// assignment
var table = 10, chair = -10;

// multiple assignment to the same value
table = chair = 0;

// multiple assignment to an array
(
var table, chair;
#table, chair = [10, -10];
#table ... chair = [10, -10, 2, 3]; // with ellipsis: now chair is [-10, 2, 3]
)

// the letters a-z are predeclared in the interpreter for interactive programming
a = 10; x = a - 8;

// variables are type-neutral and mutable: reassign to different objects
a = 10; a = [1, 2, 3]; a = nil;

// immutable variables (only in class definitions)
const z = 42; 

// lexical scope
// the closures g and h refer to different values of their c
(
f = {
	var c = 0;
	{ c = c + 1 }
};
g = f.value;
h = f.value;
c = 100; // this doesn't change it.
)

// dynamic scope: environments
f = { ~table = ~table + 1 };
Environment.use { ~table = 100; f.value }; // 101.
Environment.use { ~table = -1; f.value }; // 0.

// there is a default environment
~table = 7;
f.value;

// lexical scope in environments:
(
Environment.use {
	~table = 100;
	f = { ~table = ~table + 1 }.inEnvir;
};
)
f.value; // 101.

// because objects keep reference to other objects, references are not needed:
// objects can take the role of variables. But there is a Ref object, that just holds a value

a = Ref([1, 2, 3]); // a reference to an array, can also be written as a quote `[1, 2, 3];
f = { |x| x.value = x.value.squared }; // a function that operates on a ref
f.(a); // `[ 1, 4, 9 ]

// proxy objects serve as delegators in environments. This can be called line by line:
ProxySpace.push;
~z // returns a NodeProxy
~z.play; // play a silent sound
~z = ~x + ~y; // make it the sum of two silent sounds
~x = { PinkNoise.ar(0.1) }; // … which now are noise,
~y = { SinOsc.ar(440, 0, 0.1) }; // and a sine tone

Swift

import Foundation

// All variables declared outside of a struct/class/etc are global

// Swift is a typed language
// Swift can infer the type of variables
var str = "Hello, playground" // String
let letStr:String = "This is a constant"

// However, all variables must be initialized
// Intialize variable of type String to nil
var str1:String! // str1 is nil

// Assign value to str1
str1 = "foo bar" // str1 is foo bar

// Swift also has optional types
// Declare optional with type of String
var optionalString = Optional<String>("foo bar") // (Some "foo bar")

println(optionalString) // Optional("foo bar")

// Optionals can also be declared with the shorthand ?
var optionalString1:String? = "foo bar"

// ! can be used to force unwrap but will throw a runtime error if trying to unwrap a nil value
println(optionalString1!)

optionalString1 = nil // Is now nil
// println(optionalString1!) would now throw a runtime error

// Optional chaining can be used to gracefully fail if there is a nil value
if let value = optionalString1?.lowercaseString {
    // Is never executed
} else {
    println("optionalString1 is nil")
}

// Swift also has type aliasing
typealias MyNewType = String // MyNewType is an alias of String
var myNewTypeString = MyNewType("foo bar")

// Swift also has special types Any and AnyObject
// Any can hold any type
// AnyObject can hold any object type
let myAnyObjectString:AnyObject = "foo bar"

// Downcast myAnyObjectString to String
if let myString = myAnyObjectString as? String {
    println(myString) // foo bar
} else {
    println("myString is not a string")
}

// Swift treats functions as first-class
// Declare a variable with a type of a function that takes no args
// and returns Void
var myFunc:(() -> Void)
func showScopes() {
    // Variable is scoped to function
    let myFunctionVariable = "foo bar function"
    
    // Nested functions inherit variables declared in enclosing scope
    func nestFunc() {
        println(myFunctionVariable)
    }
    nestFunc()
}

myFunc = showScopes // myFunc is now showScopes
myFunc() // foo bar function

Tcl

Tcl's variables are local to procedures, lambdas and methods by default, and there is no initialization per se: only assignment when the variable previously did not exist.

Demonstrating:

namespace eval foo {
    # Define a procedure with two formal arguments; they are local variables
    proc bar {callerVarName argumentVar} {
        ### Associate some non-local variables with the procedure
        global globalVar;      # Variable in global namespace
        variable namespaceVar; # Variable in local (::foo) namespace
        # Access to variable in caller's context; may be local or global
        upvar 1 callerVarName callerVar

        ### Reading a variable uses the same syntax in all cases
        puts "caller's var has $callerVar"
        # But global and namespace vars can be accessed by using qualified names
        puts "global var has $globalVar which is $::globalVar"

        ### Writing a variable has no special syntax
        ### but [set] is by far the most common command for writing
        set namespaceVar $globalVar
        incr globalVar

        ### Destroying a variable is done like this
        unset argumentVar
    }
}

The main thing to note about Tcl is that the "$" syntax is a language level operator for reading a variable and not just general syntax for referring to a variable.

TI-83 BASIC

Variables will remain global, even after the program is complete. Global variables persist until deleted (or reset or power loss, unless they are archived).

Variables may be assigned with the to a value.

:1→A

TI-89 BASIC

A variable not declared local (to a program or function) is global. Global variables are grouped into folders of which one is current at any given time. Global variables persist until deleted (or reset or power loss, unless they are archived).

Local mynum, myfunc

Variables may be assigned with the or Define statements, both of which assign a new value to a variable. is typically used interactively, but only Define can assign programs or multi-statement functions.

Define mynum = 1                  © Two ways to assign a number
1 → mynum

Define myfunc(x) = (sin(x))^2     © Two ways to assign a function
(sin(x))^2 → myfunc(x)

Define myfunc(x) = Func           © Multi-statement function
  If x < 0 Then
    Return –x
  Else
    Return x
  EndIf
EndFunc

TUSCRIPT

$$ MODE TUSCRIPT,{}
var1=1, var2="b"
PRINT "var1=",var1
PRINT "var2=",var2

basket=*
DATA apples
DATA bananas
DATA cherry

LOOP n,letter="a'b'c",fruit=basket
var=CONCAT (letter,n)
SET @var=VALUE(fruit)
PRINT var,"=",@var
ENDLOOP

Output:

var1=1
var2=b
a1=apples
b2=bananas
c3=cherry

TXR

Variables have a form of pervasive dynamic scope in TXR. Each statement ("directive") of the query inherits the binding environment of the previous, invoking, or surrounding directive, as the case may be. The initial contents of the binding environment may be initialized on the interpreter's command line. The environment isn't simply a global dictionary. Each directive which modifies the environment creates a new version of the environment. When a subquery fails and TXR backtracks to some earlier directive, the original binding environment of that directive is restored, and the binding environment versions generated by backtracked portions of the query turn to garbage.

Simple example: the cases

@(cases)
hey @a
how are you
@(or)
hey @b
long time no see
@(end)

This directive has two clauses, matching two possible input cases, which have a common first line. The semantics of cases is short-circuiting: the first successful clause causes it to succeed and stop processing subsequent clauses. Suppose that the input matches the second clause. This means that the first clause will also match the first line, thereby establishing a binding for the variable a. However, the first clause fails to match on the second line, which means that it fails. The interpreter then moves to the second clause, which is tried at the original input position, under the original binding environment which is devoid of the a variable. Whichever clause of the cases is successful will pass both its environment modifications and input position increment to the next element of the query.

Under some other constructs, environments may be merged:

@(maybe)
@a bar
@(or)
foo @b
@(end)

The maybe directive matches multiple clauses such that it succeeds no matter what, even if none of the clauses succeed. Clauses which fail have no effect, but the effects of all successful clauses are merged. This means that if the input which faces the above maybe is the line "foo bar", the first clause will match and bind a to foo, and the second clause will also match and bind b to bar. The interpreter integrates these results together and the environment which emerges has both bindings.

uBasic/4tH

uBasic/4tH has two kinds of variables, 26 predefined global integer variables (named A to Z) and a maximum of 26 parameters/local integer variables (named A@ to Z@). The latter aren't defined, but merely allocated, using LOCAL(n) or PARAM(n). The first allocated local variable is assigned A@, the second B@, etc. Both parameters and local variables are freed after RETURN. Parameters are always passed by value.

' Initialization
A = 15                 ' global variable

Proc _Initialize(24)

_Initialize Param (1)
  Local (1)            ' A@ now holds 24
  B@ = 23              ' local variable
Return

' Assignment
A = 15                 ' global variable

Proc _Assign(24)

_Assign Param (1)
  Local (1)            ' A@ now holds 24
  A@ = 5               ' reassignment of parameter A@
  B@ = 23              ' local variable
Return

UNIX Shell

Works with: Bourne Shell
#!/bin/sh
# The unix shell uses typeless variables
apples=6
# pears=5+4           # Some shells cannot perform addition this way
pears = `expr 5+4`    # We use the external expr to perform the calculation
myfavourite="raspberries"

Ursa

# variable declaration
#
# declare [type] [name]
# 	-or-
# decl [type] [name]
decl int test

# initialization / assignment
#
# the set statement can be used to init variables and
# assign values to them
set test 10

# datatypes
#
# ursa currently has 10 built-in types, but
# more may be added in the future. 
#     boolean
#     double
#     file
#     function
#     int
#     iodevice
#     port
#     serverport
#     string
#     task
#
# also, java classes may be used as data types
# cygnus/x ursa

# scope
#
# there is a global variable space, and functions
# have their own scope. control statements (for,
# if, try, while) don't have their own scope yet,
# but this will be implemented in the near future

# referencing
#
# variables are referenced by their name
decl port p
out p endl console

VBA


Data types

Data typeRuntime
type
CategoryStorage
allocation
Value range
BooleanBoolean1 byteTrue or False
IntegerInt16signed
integer
2 bytes-32768 through 32767
LongInt32signed
long integer
4 bytes-2,1E+9 through 2,1E+9


precision: 9 decimals

ByteUInt8unsigned
byte
1 byte0 through 255
SingleSinglesingle precision
floating point
4 bytes -3.45E+38 through -1.4E-45 for negative values;


1.4E-45 through 3.4E+38 for positive values;
precision: 6 decimals

DoubleDoubledouble precision
floating point
8 bytes -1.7E+308 through -4.9E-324 for negative values;


4.9E-324 through 1.7E+308 for positive values;
precision: 16 decimals

CurrencyDecimalextended precision
fixed point
8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807


0 through ±9.2E14
smallest nonzero number is 0.0001;

String(n)Stringfixed length
string
n bytes
StringStringvariable length
string
10+(string length)
DateDateTime8 bytes00:00:00 AM on January 1, 0001 through
11:59:59 PM on December 31, 9999
VariantVariant4 bytesStores any type of data

VBScript


Data types

VBScript has only one data type called a Variant.

SubtypeCategoryValue range
BooleanTrue or False
Integersigned
integer
-32768 through 32767
Longsigned
long integer
-2,1E+9 through 2,1E+9
precision: 9 decimals
Byteunsigned
byte
0 through 255
Singlesingle precision
floating point
-3.45E+38 through -1.4E-45 for negative values;
1.4E-45 through 3.4E+38 for positive values;
precision: 6 decimals
Doubledouble precision
floating point
-1.7E+308 through -4.9E-324 for negative values;
4.9E-324 through 1.7E+308 for positive values;
precision: 16 decimals
Currencyextended precision
fixed point
-922,337,203,685,477.5808 to 922,337,203,685,477.5807
0 through ±9.2E14
smallest nonzero number is 0.0001;
Stringvariable length
string
Date00:00:00 AM on January 1, 0001 through
11:59:59 PM on December 31, 9999
ObjectStores any object reference
EmptyVariant is uninitialized
NullVariant has no valid data
ErrorVariant contains an error number

Visual Basic


Data types

Data typeRuntime
type
CategoryStorage
allocation
Value range
BooleanBoolean1 byteTrue or False
IntegerInt16signed
integer
2 bytes-32768 through 32767
LongInt32signed
long integer
4 bytes-2,1E+9 through 2,1E+9


precision: 9 decimals

ByteUInt8unsigned
byte
1 byte0 through 255
SingleSinglesingle precision
floating point
4 bytes -3.45E+38 through -1.4E-45 for negative values;


1.4E-45 through 3.4E+38 for positive values;
precision: 6 decimals

DoubleDoubledouble precision
floating point
8 bytes -1.7E+308 through -4.9E-324 for negative values;


4.9E-324 through 1.7E+308 for positive values;
precision: 16 decimals

CurrencyDecimalextended precision
fixed point
8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807


0 through ±9.2E14
smallest nonzero number is 0.0001;

String(n)Stringfixed length
string
n bytes
StringStringvariable length
string
10+(string length)
DateDateTime8 bytes00:00:00 AM on January 1, 0001 through
11:59:59 PM on December 31, 9999
VariantVariant4 bytesStores any type of data
ObjectObject4 bytesStores any object reference

Visual Basic .NET


Variable declaration

Dim variable As datatype
Dim var1,var2,... As datatype

example:

Dim n1,n2 as Integer
Dim x as Double
Dim isRaining as Boolean
Dim greeting as String


Initialization

Dim variable As datatype = value
Dim var1,var2,... As datatype

example:

Dim wholeNumber1,wholeNumber2 as Integer = 3
Dim realNumber as Double = 3.0
Dim isRaining as Boolean = False
Dim greeting as String = "Hello, this is World speaking."
Dim longArray() As Long = {0, 1, 2, 3}  
Dim twoDimensions(,) As Integer = {{0, 1, 2}, {10, 11, 12}}


Assignment

variable = expression
         v = a
         d = b^2 - 4*a*c
         s3 = s1 & mid(s2,3,2)
variable <operator>= expression2
         c += a
         c -= a
         c *= a
         c /= a
         c ^= a
         c <<= n
         c >>= n
         c &= a


Data types

Data typeRuntime
type
CategoryStorage
allocation
Value range
BooleanBoolean1 byteTrue or False
CharCharunsigned2 bytes0 through 65535
SByteInt8signed
byte
1 byte-128 through 127
ShortInt16signed
short integer
2 bytes-32,768 through 32,767
IntegerInt32signed
integer
4 bytes-2,1E+9 through 2,1E+9
precision: 9 decimals
LongInt64signed
long integer
8 bytes-9.2E+18 through 9.2E+18
precision: 18 decimals
ByteUInt8unsigned
byte
1 byte0 through 255
UShortUInt16unsigned
short integer
2 bytes0 through 65,535
UIntegerUInt32unsigned
integer
4 bytes0 through 4,2E+9
ULongUInt64unsigned
long integer
8 bytes0 through 1.8E+19
SingleSinglesingle precision
floating point
4 bytes -3.45E+38 through -1.4E-45 for negative values;
1.4E-45 through 3.4E+38 for positive values;
precision: 6 decimals
DoubleDoubledouble precision
floating point
8 bytes -1.7E+308 through -4.9E-324 for negative values;
4.9E-324 through 1.7E+308 for positive values;
precision: 16 decimals
DecimalDecimalextended precision
fixed point
16 bytes 0 through ±7.9E+28 with no decimal point;
0 through ±7.9 with 28 places to the right of the decimal;
smallest nonzero number is ±1E-28;
precision: 28 decimals
StringStringvariable length
string
DateDateTime8 bytes00:00:00 AM on January 1, 0001 through
11:59:59 PM on December 31, 9999
ObjectObject4 bytes on
32-bit platform
8 bytes on
64-bit platform


scope
By default the scope of a variable is local to the sub, function or module. By default the scope of a sub or function is global to the project. Attributes Public or Private can mofidy these scopes.

V (Vlang)

name := 'Bob'
age := 20
large_number := i64(9999999999)

Variables are declared and initialized with :=. This is the only way to declare variables in V. This means that variables always have an initial value.

The variable's type is inferred from the value on the right hand side. To choose a different type, use type conversion: the expression T(v) converts the value v to the type T.

Unlike most other languages, V only allows defining variables in functions. Global (module level) variables are not allowed. There's no global state in V (see Pure functions by default for details).

For consistency across different code bases, all variable and function names must use the snake_case style, as opposed to type names, which must use PascalCase.

Mutable variables

mut age := 20
println(age) // 20
age = 21
println(age) // 21

To change the value of the variable use =. In V, variables are immutable by default. To be able to change the value of the variable, you have to declare it with mut.

Initialization vs assignment Note the (important) difference between := and =. := is used for declaring and initializing, = is used for assigning.

The values of multiple variables can be changed in one line. In this way, their values can be swapped without an intermediary variable.

mut a := 0
mut b := 1
println('$a, $b') // 0, 1
a, b = b, a
println('$a, $b') // 1, 0

Declaration errors In development mode the compiler will warn you that you haven't used the variable (you'll get an "unused variable" warning). In production mode (enabled by passing the -prod flag to v – v -prod foo.v) it will not compile at all (like in Go).

fn main() {
	a := 10
	if true {
		a := 20 // error: redefinition of `a`
	}
	// warning: unused variable `a`
}

Unlike most languages, variable shadowing is not allowed. Declaring a variable with a name that is already used in a parent scope will cause a compilation error.

WDTE

WDTE does not have variables, per se, but it does have several things that work similarly. The most obvious is function parameters:

let example t => io.writeln io.stdout t;

The parameters are scoped to the inside of the function in which they're declared. There are also lambdas, which also have parameters:

let example t => 3 -> (@ s n => * t n);

Lambdas are closures, so they have access to the parameters of the function that called them. The above multiplies 3 by the value of t.

There are also 'slots' for chains. Chains are essentially a series of expressions that are all 'chained' together. They take advantage of the fact that everything in WDTE is a function. In a chain, the first is called, then the second, and then the return of the second is called with the return of the first as an argument, then the third is called, and then its return is called with the output of the previous segment, and so on. In each element of the chain, a 'slot' can be specified which serves as a named binding for accessing that element's return value later. For example:

let writeToFile name data =>
  file.open name : f
  -> str.format '{}' data
  -> io.writeln f
  -- io.close f
  ;

In this example, first file.open name is called. After this, the return value of that call can be accessed later in the chain using f. This is done in the last element as a way of closing the file.

Finally, an expression can also be bound to a name using a let expression, like in the function declarations shown above, as assigning a value to a function with no parameters is similar to creating a constant. When a let is encountered, all following expressions in the current block of code are run under a subscope of the current scope, which gives much the same effect as a variable assignment. For example:

let a => 3;
io.writeln io.stdout a;

Wren

Wren is dynamically typed and variables can be assigned or reassigned values of any type.

If a variable is declared but not assigned a value, it is given the special value 'null'.

A variable can only be declared once for a given scope (top level, block, function, method etc.) but the same variable name can be reused in a nested scope in which case it will hide the variable in the outer scope.

Depending on the type of value assigned, a variable can either hold the value directly (value type) or a reference to where an object is stored in memory (reference type). Values of the built-in types: Num, Bool and Null are value types and all other types are reference types. However, values of the built-in String, Class and Range types are immutable and are always copied/compared by value rather than by reference. Consequently they behave as though they were value types.

Here are a few examples.

var a           // declares the variable 'a' with the default value of 'null'
a = 1           // initializes 'a'
a = "a"         // assigns a different value to 'a'
var b = 2       // declares and initializes 'b' at the same time
b = true        // assigns a different value to 'b'
var c = [3, 4]  // 'c' is assigned a List which is a reference type
c = false       // 'c' is reassigned a Bool which is a value type

var d = 1       // declaration at top level
{
    var d = 2   // declaration within a nested scope, hides top level 'd'
    var e = 3   // not visible outside its scope i.e this block
}
System.print(d) // prints the value of the top level 'd'
System.print(e) // compiler error : Variable is used but not defined

XPL0

There are only three variable types: 32-bit signed integers (in the
32-bit version), IEEE-754 64-bit reals, and characters which are actually
32-bit addresses of (or pointers to) strings. When a 'char' variable is
subscripted, it accesses a single byte. All variable names must be
declared before they are used, for example:

int  I, J, K;
real X, Y, Array(10);
char String(80);

Variables (as well as all declared names, such as for procedures) must
start with a capital letter or underline. Names may contain digits or
underlines and be any length, but only the first 16 characters are
significant. Names are case-insensitive by default, unless the /c switch
is used when compiling.

Variables are usually assigned values like this: X:= 3.14, but the first
declared variables in a procedure can have argument values passed into
them. Variables other than arguments are only initialized by explicit
assignments in the body of the code.

Local variables are encapsulated in the procedures that declare them and
are not visible (are out of scope) outside those procedures. Procedures
can be (statically) nested several levels deep. The deepest procedure can
"see" all variables declared in the procedures in which it is nested. If
two variables are declared with identical names, the most local one is
used.

XSLT

Although called variables, XSLT "variable" elements are single-assignment, and so behave more like constants. They are valid in the node scope in which they are declared.

<xsl:variable name="foo" select="XPath expression" />
<xsl:if test="$foo = 4">... </xsl:if>     <!-- prepend '$' to reference a variable or parameter-->

Z80 Assembly

Like in 6502 Assembly, variables need not be declared per se. Any memory location can be given a label and a value can be stored at that memory location and retrieved from it.

UserRam equ &C000

ld a,&50       ;load hexadecimal 50 into A
ld (UserRam),a ;initialize UserRam with a value of &50

ld a,&40       ;load hexadecimal 40 into A
ld (UserRam),a ;assign UserRam a new value of &40

Z80 has two data types: byte and word. Words are little-endian, meaning that the "low byte" is stored first. The two statements below are identical:

byte &EF,&BE
word &BEEF

Bytes can be loaded into 8-bit registers, and words can be loaded into 16-bit register pairs. A value can be treated as a pointer to a memory location by enclosing it in brackets, which dereferences the pointer before loading from it:

org &8000

ld hl,TestData   ;H = &90, L = &00

ld hl,(TestData) ;H = &BE, L = &EF

;In a real program you would need something here to stop the program counter from executing the data below as instructions.
;For simplicity this was left out.

org &9000
TestData:
byte &EF,&BE

Scope does not exist in Z80 Assembly by default, unless enforced by the assembler itself. Assemblers that do not support local labels will require all labels to be unique.

zkl

The are two variable type in zkl, register (auto in C) and var (instance variable). vars are global to class [instance], registers are visible to their scope and enclosed scopes. In addition, a function can have vars (static var in C), which are actually [hidden] instance data. vars have no type.

var v;  // global to the class that encloses this file
class C{ var v }  // global to class C, each instance gets a new v
class C{fcn f{var v=123;}} // v can only be seen by f, initialized when C is
class C{fcn init{var [const] v=5;}} // init is part of the constructor,
   so vars are promoted yo class scope. This allows const vars to be created at
   construction time
var v=123; v="hoho"; //not typed
class C{var v} // C.v OK, but just v is not found
class C{var[const]v=4} // C.v=3 illegal (compile or run time, depending)
class C{var[mixin]v=4} // the compiler treats v as an int for type checking
class C{var[proxy]v=f; fcn f{println("my name is ",self.fcn.name)} }
   v acts like a property to run f so C.v is the same as C.f()
class C{reg r}         // C.r is compile time error
r:=5;  // := syntax is same as "reg r=5", convenience

Zoea

program: variables
# The concept of variables is completely alien in zoea:
# there is no support for variables and no way of defining or using them.
# Instead programs are described by giving examples of input and output values.

Zoea Visual

Variables