Section 4.2 - Subprogram Declarations and Parameters

Let's see how to declare a subprogram (procedure or function) declaration. The main difference between a procedure and function is that a function returns a value, while a procedure does not (though a procedure can change the values of parameters sent to it). Here's an example of a procedure declaration for a procedure named Average, which takes as input two values (called A and B) and changes a third variable (called Result, presumably to hold the average):

procedure Average(A, B : in Integer; Result : out Integer);

Actually, a subprogram that averages two numbers would probably be defined as a function. Here's a declaration of a function which takes as input two values and returns a result:

function Average_Two(A, B : in Integer) return Integer;

Note the keywords `in' and `out'; this indicates the mode of the parameter. There are three possible modes:

  1. `in' - the parameter's value may be used but not changed.
  2. `out' - the parameter's value may be changed but not used.
  3. `in out' - the parameter's value may be used and/or changed.

The default mode is `in', but I recommend that you always state the desired mode.

Here's a BNF for subprogram declarations:

subprogram_declaration ::= subprogram_specification ";"

subprogram_specification ::= "procedure" procedure_name parameter_profile | 
                 "function" procedure_name parameter_profile "return" type

parameter_profile ::= [ "(" parameter_specification
                            { ";" parameter_specification} ")" ]

parameter_specification ::= parameter_name_list ":" mode type
                            [ ":=" default_expression ]

mode ::= [ "in" ] | "out" | "in" "out"

parameter_name_list ::= identifier { "," identifier }

procedure_name ::= identifier

Quiz:

Which of the following is not a legal subprogram declaration?

  1. procedure Delete_File( in Integer : A );
  2. procedure Initialize;
  3. function Middle_Value( A, B, C : in Integer ) return Integer;

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 4 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s4s2.htm".