Procedures and Functions Description

Syntax of Procedures

$ ProcedureDeclaration = ProcedureHeading ";" ProcedureBody

$ ProcedureHeading = SUB ident “(“ [FormalParameters] “)”

$ ProcedureBody = [ DeclarationSequence ] BEGIN StatementSequence

END SUB ident ";"

Syntax of Functions

$ FunctionDeclaration = FunctionHeading ";" FunctionBody

$ FunctionHeading = FUNCTION ident “(“ [FormalParameters] “)” “:”qualident

$ FunctionBody = [ DeclarationSequence ] BEGIN StatementSequence

END FUNCTION ident ";"

 

$ DeclarationSequence = Declaration { Declaration }

$ Declaration = ( { ConstantDeclaration } | { VariableDeclaration } | { ProcedureDeclaration } | { FunctionDeclaration } )

Description

Procedures are used to specify the set of actions to modify the external (referring to them) program environment. An example of such modification is a "New Variables Value Syntax", "Save Data to an External File", and so on. Define the procedure name inside the program, where its operators will be executed, to call the procedure.

Functions are used to specify an algorithm for some value calculation. In this case functions are similar to expressions, which also calculate values. That is why a function call is one of available expression operands, which defines the value that the function calculates (returns).

A procedure or function description contains a header and a body. A subprogram header describes its name and formal parameters. A function header also contains the result value type. The subprogram body contains local descriptions and operators.

A function must return some result. The RETURN operator with a function result parameter is used to return a function value. Use the RETURN operator without parameters to exit a procedure.

All constants, variables, procedures, and functions described in a subprogram body are local for this subprogram.

In addition to local objects and formal parameters, the objects, defined in the external (referring to the program) unit, are also visible in the body; except for those objects, which have the same identifiers as local objects and subprogram parameters have.

Use the subprogram identifier inside the body to call the subprogram recursively.

A procedure or function prior description is absent. Any procedure or function may be called if it is described in the current block, or in any of the enclosing blocks.

Example

Procedure creation and application:

Sub Main;

//Declaration of SimpleSub procedure with three formal variables

Sub SimpleSub(a, b: Integer; var c: Integer);

Begin //SimpleSub procedure text start

c := a + b;

End Sub SimpleSub; //SimpleSub procedure text end

Var

a: Integer; // Declaration of variables for the Main unit

Begin

SimpleSub(1, 3, a); //SubSample procedure call

End Sub Main;

Function creation and application:

Function Func1(a, b: Integer): Double;

Var //Declaration of variables for the Func1 function

c: Double;

Begin //Function Func1 text start

c := (a + b) / 2;

Return c; // Function Func1 result

End Function Func1; //Function Func1 end

Var //Declaration of variables for the FunctionSample unit

c: Double;

Begin //Program text start

c := Func1(4, 5); //Function Func1 application

End Sub Main;

See also:

Formal Parameters | Descriptions and Syntax Rules