Language Syntax

Procedures


Procedures are the basic units of SPP programs. They also include functions, procedures that return a value. The form of a procedure declaration is shown below.

[data_type] procedure proc_name ([p1 [, p2 [,... ]]])
[declarations for procedure arguments] 
[declarations for local variables] 
[declarations for functions] 
[initialization] 	
begin
      [executable statements] 	
end 
The data_type field must be included if the procedure returns a value. The
begin keyword separates the declarations section from the executable body of the procedure, and is required. The end keyword must follow the last executable statement. Note that the procedure statement and the declaration statements must begin in the first character on the line.

All parameters, variables, and typed procedures must be declared. The SPP language does not permit implicit typing of parameters, variables, or procedures, unlike Fortran. By convention, declarations of procedure arguments precede local declarations. It is also good practice to use in-line comments to describe the declarations.

If a procedure has formal parameters, they should agree in both number and type in the procedure declaration and when the procedure is called. In particular, beware of short or char parameters in argument lists. An int may be passed as a parameter to a procedure expecting a short integer on some machines, but this usage is not portable, and is not detected by the compiler. The compiler does not verify that a procedure is declared and used consistently.

If a procedure returns a value it is known as a function and the calling program must declare the procedure in a type declaration, and must reference the procedure in an expression. The function procedure must contain a return which assigns the value to pass back to the caller as the function value. A function procedure may return a numerical value, but may not return an array or string.

If a procedure does not return a value, the calling program may reference the procedure only in a call statement. However, the return statement may be used to end the procedure at any point and return control to the calling procedure.

begin...end

The executable statements in a procedure must be surrounded by begin and end statements. All declarations must be placed between the procedure statement and the begin.

{...}

Braces ({ and }) may be used to bracket explicitly groups of statements intended to be treated as a single statement, for example, in if, for, or while constructs.

Arguments

Formal or dummy arguments and actual arguments must match in number and type. That is, the declarations in the calling and called procedure must be the same for all of the arguments.

entry Statement

Procedures with multiple entry points are permitted in SPP because they provide an alternative to global common when several procedures must access the same data. The multiple entry point mechanism is similar to block structuring. The multiple entry point construct is only useful for small problems. If the problem grows too large, an enormous procedure with many entry points may result, which is difficult to maintain. The form of a procedure with multiple entry points is shown below. Either all entry points should be untyped, as in the example, or all entry points should return values of the same type. Control should only flow forward. Each entry point should be terminated by a return statement, or by a goto to a common section of code which all entry points share. The shared section of code should be terminated by a single return which all entry points share.

Intrinsic Functions

Any function written as part of the task must be declared. However, SPP includes several intrinsic functions that need not be declared. The intrinsic functions are generic functions, meaning that the same function name may be used regardless of the data type of the arguments. The arguments to trigonometric functions are assumed to be in radians, as in Fortran.



Table 1.10: Intrinsic Functions

Note that the names of the type coercion functions (char, short, int, real, etc.) are the same as the names of the data types in declaration statements. The functions log10, tan, and the hyperbolic functions may not be called with complex arguments. As in Fortran, the arguments to trigonometric functions must be in radians.

Calling Fortran Subprograms

Since SPP is preprocessed into Fortran, in most cases, it is quite straightforward to call an existing Fortran subroutine from an SPP procedure. The most important caution is in using character strings. SPP strings are not the same as Fortran strings. SPP strings are implemented as arrays of integers. However, there are procedures available to transform between the two: f77pak() converts an SPP string to a Fortran string, and f77upk() converts a Fortran string to an SPP string. Note that you must declare the Fortran string in the SPP procedure with a Fortan statement. This is possible with the % escape as the first character on a line. This indicates to the xc compiler that the following statement should not be processed but copied directly to the Fortran code. See also "Expressions" on page 31 and "Fortran Strings" on page 125.

begin...end
{...}
Arguments
entry Statement
Intrinsic Functions
Table 1.10: - Intrinsic Functions
Calling Fortran Subprograms

Generated with CERN WebMaker