Writing Procedures

REXX procedures provide a facility for modularizing programs. REXX procedures are similar in concept to the function and subroutine facilities of other languages.

Advantages of using procedures:


Re-use of code

Programs are typically composed of repetitions of the same or very similar tasks. Partitioning of your program into procedures allows a given task to be isolated. Writing and debugging your program is much easier. Experienced programmers typically use more procedures than novice programmers.

In the following snip of code Lookup could be a massive or trivial block of code. In either case correcting or augmenting the program is more manageable because any change to Lookup will be propagated throughout the overall program.


       say Lookup( A )

       say Lookup( B )

       say Lookup( C )

       say "Last one:" Lookup( D )

Re-use does not need to be limited to the current program. Particularly useful procedures can and should be added to the programmers library and re-used in future projects.


Local Variables

Variable names used within procedures are unique and will not interfere with like named variables used in the main program or other procedures.


         .
         .
     A = 4
     B = Factorial( A )
     Say A B
         .
         .
     Procedure: Factorial
     A = 24
     Return ( A )


Structured Programming

Structured programming aids development and maintenance by separating overall program flow and the details of processing. All of the detailed execution is done in procedures. If your "toolbox" is well stocked, a program can often be a simple knitting of procedures drawn from a tested library of routines.

The following example is very simple, but if This, That, and Other were each several hundred lines of code, the overall simplicity would be buried in the details. Adding another option in the select block would be arduous, rather than trivial.

Another advantage offered by a structured approach is that the procedures can be written and tested (possibly by different individuals) in isolation. Dummy procedures can be used to test the main logic. Inserting previously tested procedures into a tested mainline, enhances the likelihood of a bug free result.


     /* My Program */
     call Initialize
     do until( ... )
          select
             when ( A = 1 ) then call This
             when ( A = 2 ) then call That
             otherwise call Other
          end
          call Display_results
     end
     exit

     procedure: This
     procedure: That
     procedure: Other
     procedure: Initialize
     


Using Procedures

There are two styles of invoking a procedure. A procedure call, similar to a subroutine call in some high level languages, does not return a value. Inline execution returns a value. (And will be flagged as an error if no value is returned.)


        .
        .

     call My_Subroutine A, B, C

        .
        .

     A = My_Function( A, B, C )
     


Writing Procedures

Procedures begin with the prodecure: and end with the return keywords.


     My_Routine: procedure expose B

          parse arg A , C , D , E

          F = A + B + C + D + E

          return ( f )
     

Notes:

  1. The arguments are passed, by value, to the procedure as a character string. The procedure is responsible for parsing the arguments.

  2. Because the arguments are passed by value, the procedure cannot return a result by modifying an argument.

  3. Only a single value of a compound variable can be passed as an argument.

  4. Only a single value can be returned.


An Example

The following program is a bit controversial in computer science circles.


     /* Compute factorials */

     say Factorial( ARG(1) )

     EXIT

     Factorial: PROCEDURE

          PARSE ARG N

          SELECT
             WHEN ( N >= 1 ) THEN RETURN ( N * Factorial( N - 1 ) )
             WHEN ( N = 0 ) THEN RETURN ( 1 )
             OTHERWISE RETURN ( 0 )
          END
     


Rexx Group Charter
Lesson 1 | Lesson 2 | Lesson 3 | Lesson 4
Exercises 1 | Exercises 2 | Exercises 3
Answers to exercises
Exercises 1
Example code
Stock Market | Stock Market_a


About these pages
Content updated: 07-09-2005 08:51am

Page building by: PPWizard Content updated
07-09-2005 08:51am
Valid HTML 4.01!Valid CSS!