SUBPROG

Contents


Subprog is used to declare a sub-programme that may include a list of arguments that can be called by Call.

 

Syntax

   Subprog sub_prog [ '(' list_args ')' ]

 

Parameters

Element

Description

Restrictions

sub_prog

nom_d'OBJet indicating the sub-programme.

None.

list_args

List of nom_de_variables separated by the character ','.

The arguments must have different names.

 

Examples

# Calling a counter increment sub-programme in the
# current routine
    Call INCR_CPT(10,OK)
   ...
# Further on in the same routine…
    Subprog INCR_CPT(DELTA,ST)
       Value Integer DELTA
       Variable Libelle ST
       Lock COMPTEUR With lockwait = 5
       ST = [S]fstat
       If ! ST
          [C]COMPTEUR += DELTA
          Unlock COMPTEUR
       Endif
    End

 

Description and comments

A sub-programme declared by the Subprog instruction will end with the End instruction. Its execution, initiated by a Call, actually ends as soon as an End instruction is found. The routine that called with sub-programme then resumes at the instruction following the Call.

When the sub-programme includes arguments, it is possible, but not essential to declare some or all of them following the instruction Subprog. The order in which this is done does not have to be that of the instruction Subprog.

Apart from their type and dimension, it shall also be specified if they have passed by address or value, using one of the following keywords:

·         Value means that the argument is passed by value and copied to the local variable of the sub-programme created for this. This will be the default mode when the argument passed is an expression. An argument passed by value is therefore not modified by the sub-programme. This mode is a heavy user of memory space and run time.

·         Variable means the argument is passed by address. The local variable created points to the variable passed in the parameter, which may therefore be modified by the sub-programme.

·         Const means that the argument has been passed by address but that the local variable created will not be modifiable (nor assignable). This is the default mode when the argument passed is a variable.

The same arguments may be used as for the variables (Libelle, Shortint, Integer, Date, Decimal and Char).
By default, the type will be:

·         Integer if the argument is Libelle, Shortint or Integer type;

·         otherwise the same type as the argument.

Example:

Local Integer A
A = 1
Call EXP1(A,2,3)
End

Subprog EXP1(A,B,C)
Variable Integer A
Value Integer B, C
Infbox num$(type(A))-num$(A)-num$(B)-num$(C):
# on affichera 4 1 2 3
End

The length of the Char type arguments will not be specified. It will be enough to follow the name of the argument with (). The length of the local variable will be the same as the argument passed by Call.

After the type, it is essential to specify the dimension if there is one, using the characters "(" and ")". Use commas to separate several dimensions when declaring them.

Example: Decimal value MAT(,) declares a two-dimensional table of BCD numbers whose size depend on the argument passed by Call and whose lower limits for both dimensions will be 0.

By default – that is, if nothing else is specified – the size of each dimension will be that of the argument and its lower limit shall be 0. The size of each dimension may also be specified by an integer value expression or a variable also passed in parameter. In the latter case, this variable must be declared first.

Example: Char variable ARGS () (AC,GV)
declares a two-dimensional character string table, the first dimension being from 0 to AC - 1, the second from 0 to GV -1.

The lower limit may be specified. If the lower limit has been specified, the upper limit may also be specified. If this is not done, the resultant size will be that of the argument. These limits may be integer value expressions, variables. The lower limits shall be followed by "..".

Example: Integer value CUB(N11..,N21..N22,) declares an integer table in three dimensions, indexed from N11 for the first, N21 to N22 for the second and from 0 for the third.

If the limits are specified, these do not have to be identical to those of the calling routine arguments, the smallest index element of the Call argument shall be associated with (or copied to) the smallest index element of the local variable of the sub-programme and so on.

Example:

Integer T(1..M, 3..N)
Call SP(T)

Subprog SP (TAB2)
Value Integer TAB2(,)

In this case, the TAB2(0,0) element will point to T(1,3), etc.

When a sub-programme starts up, a local variable class is created. The variables declared in the sub-programme shall be from this class by default. It will be automatically destroyed at the final End.

While running a sub-programme may access the global variables and Adonix OBJects of the calling routines. In order to not to modify the context, on returning from the sub-programme called by Call, the OBJects for this sub-programme shall be declared in Local mode (Local File, Local Mask, Local Mesbox, Local Inpbox ). It will be automatically closed at the final End.

 

Comments

Creation of a local clause means that the sub-programme called by Call always takes longer to run than a label called by Gosub. The sub-programmes on the other hand provide better modularity and better readability.

When a sub-programme is running, the following information is stored:

·         Adonix system and global variables,

·         open files masks and dialogue boxes,

·         open sequential files,

·         the default class lists (except local class),

·         Inter or Nointer mode,

·         initiation of a transaction.

Conversely, the following are lost:

·         the local variables class,

·         the label for Onerrgo, as well as that for Onintgo.

On the other hand, a sub-programme may not terminate a transaction it did not itself initiate.

 

Associated errors

Error

Description

ERRET (32)

Transaction initiated in another programme.

ERDIM (55)

Number of dimensions incorrect

ERVEX (61)

Variable already exists.

 

Associated key words

CALL - VALUE - VARIABLE - CONST - END - MASK - FILE - INPBOX - MESBOX


CONTENTS


Copyright © Sage 1999 - 2007