program |
declarations |
declarations |
declaration [
declarations![]() |
declaration |
subprogram_decl ![]() |
var_declaration_list |
var_declaration ;
[ var_declaration_list![]() |
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.
var_declaration |
identifier_list : type |
identifier_list |
identifier [
, identifier_list![]() |
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.
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.
subprogram_decl |
subprogram_header ;
![]() |
subprogram_header |
procedure_header
![]() |
procedure_header |
procedure name_and_formals |
function_header |
function name_and_formals : type |
name_and_formals |
identifier ( [ formal_parameters ] ) |
formal_parameters |
formal_parameter [
; formal_parameters![]() |
formal_parameter |
[ 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:
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 2010-01-08