Subsections

Declarations

program
$\rightarrow$
declarations

declarations
$\rightarrow$
declaration [ declarations$_1$ ]

declaration
$\rightarrow$
subprogram_decl $\vert$ var var_declaration_list

var_declaration_list
$\rightarrow$
var_declaration ; [ var_declaration_list$_1$ ]

A program consists of at least one declaration. There are two types of declarations: subprogram declarations and variable declarations. The order of declarations is not important: a subprogram or variable may be used before or after it has been textually declared.

Variable declarations

var_declaration
$\rightarrow$
identifier_list : type

identifier_list
$\rightarrow$
identifier [ , identifier_list$_1$ ]

A var_declaration is used to declare a variable in the current scope. Multiple variables can be declared in one declaration.

There are three storage classes: bss, stack, and heap data. There are also three types of variables: global, formal, and local. Whether it is a global, formal, or local variable declaration depends on the context.

global
A global variable can only be declared in global scope. Its storage class is bss. Before main is executed, all global variables are initialized according to Table 1. The lifetime of the variable spans the execution of main.

formal
A formal parameter can only be declared in a parameter list. If identifier_list is prefixed by the keyword var, call by reference semantics is used when the subprogram is invoked, otherwise call by value semantics is used. Its storage class is stack. The lifetime spans the execution of the subprogram.

local
A local variable can only be declared in a compound statement. Its storage class is stack. The lifetime spans the execution of the compound statement.

A variable may not be declared multiple times in the same scope; neither may it be redeclared as subprogram or vice versa (variables and subprograms share the same, scoped name space). If it is redeclared in a nested scope, it hides the variable (or subprogram) in the surrounding scope.

A variable has either a value of a basic type, or a reference to an array.

Variables are always implicitly initialized upon declaration. The default value depends on the type, as is shown in Table 1.


Table 1: Default initialization values.
type value
array of any type null
bool false
char '$\backslash$0'
int 0
real 0.0


Subprogram declarations

subprogram_decl
$\rightarrow$
subprogram_header ; $\vert$ subprogram_header is statement

subprogram_header
$\rightarrow$
procedure_header $\vert$ function_header

procedure_header
$\rightarrow$
procedure name_and_formals

function_header
$\rightarrow$
function name_and_formals : type

name_and_formals
$\rightarrow$
identifier ( [ formal_parameters ] )

formal_parameters
$\rightarrow$
formal_parameter [ ; formal_parameters$_1$ ]

formal_parameter
$\rightarrow$
[ var ] var_declaration

There are two types of subprogram declarations: procedures and functions. Functions return a value, while procedures do not. If no return statement is executed within a function body, then a compiler or run-time error will occur.

A subprogram declaration may come with or without body. A subprogram may only be declared in the global scope. It may be redeclared, provided that:

If no declaration of a subprogram contains a body, then the function must be a standard library function, or a linker error will occur.

The subprogram main is special: the runtime system calls this function to start execution of the program. It must be a function returning an int and accepting one call by value argument. The type of the argument must be array of string (or, equivalently, array of array of char). The actual value is a list of the command line arguments. The array has a size of at least one string; the first string is the name of the executable with which the program was invoked. Deleting the array or one of its elements causes undefined behavior. The return value of main is used as exit status of the program. Successful program execution should result in a zero return value; unsuccessful program execution should result in a non-zero return value.

K.G. Langendoen 2006-10-03