Progress
Language Tutorial
for Character


Using Parameters to Pass Values

Shared variables are one way that procedures share information, although perhaps not the best way. It may not be clear to other programmers who read your code what relationship each procedure has to the shared variable. Another way of sharing values is to pass parameters. A parameter is a variable in a called procedure whose function is to accept an input value from the calling procedure, output a value to the calling procedure, or both. For example, you can use a parameter with an internal procedure to create something like a function that accepts input values and outputs return values. If you define such a procedure, you can use it many times in the main procedure.

You define a parameter with the DEFINE PARAMETER statement.

SYNTAX
DEFINE
  { INPUT | OUTPUT | INPUT-OUTPUT } PARAMETER parameter 
    { AS data-type | LIKE field  }
    { [ format-phrase ] } 

The statement options are similar to the options you used with the DEFINE VARIABLE statement. The parameter types are:

All procedure and function parameters (that is, pre-defined and user-defined functions) are passed by value. This means that for any INPUT-OUTPUT or OUTPUT parameter, the field or variable that receives the output value is not set by the called procedure until and unless the procedure returns without error.

To call a procedure that contains parameters, place a comma-separated list of parameters enclosed in parentheses at the end of the RUN statement. You can specify a type for each parameter in the list. Similarly, to call either type of function, you also define a comma-separated list of parameters enclosed in parentheses within a statement that invokes the function.

Parameters Used in the RUN Statement

The following syntax shows where to define parameters used within a RUN statement.

SYNTAX
RUN procedure-file
[ ( [ { INPUT | OUTPUT | INPUT-OUTPUT } expression ] , ... ) ] 

Here’s is a sample RUN statement:

RUN proc2.p (INPUT-OUTPUT Variable1, INPUT "Constant", OUTPUT Variable2). 

If you do not specify the parameter type keywords in the RUN statement, Progress assumes that the parameters are INPUT parameters. The number and type of parameters in the called procedure must match the number and type of procedures in the RUN statement:

DEFINE INPUT-OUTPUT PARAMETER Param1 AS CHARACTER.
DEFINE INPUT PARAMETER Param2 AS CHARACTER.
DEFINE OUTPUT PARAMETER Param3 AS CHARACTER.
        .
        .
        .
DISPLAY Param1 Param2 Param3 WITH FRAME Frame1. 

The called procedure matches the values passed by the RUN statement by associating the first value to the first defined parameter and so on.

Parameters Used in the FUNCTION Statement

The following is a partial syntax of the FUNCTION statement. It presents the syntax that you can use to define parameters for a user-defined function using the FUNCTION statement.

SYNTAX
FUNCTION function-name
  [ RETURNS ] data-type
  { { [ INPUT ] |  OUTPUT | INPUT-OUTPUT }
       param-name AS data-type
   } 

The FUNCTION statement matches parameters by associating the first value to the first defined parameter, the second value to the second defined parameter, and so on. The number and type of parameters in the calling procedure must match the number and type of procedures in the FUNCTION statement.

For example, in the user-defined function code presented in Figure 6–3 earlier in this chapter, the function called compute is written to accept two input parameters defined as INTEGER, and one output parameter that is also defined as INTEGER. In the first invocation of the function, the actual input parameter values 3 and 3 replace the input parameter values hr and rate. Within the function definition, these two values are multiplied (hrs * rate) and the function returns the result, 9, to the calling procedure. The output parameter for_tax is also returned to the calling procedure.

The second invocation in Figure 6–3 shows how this code is re-usable. The second invocation provides two different input parameter values, 7 and 5, but the same operation defined for the function is performed.

Follow these steps to demonstrate passing parameters and shared variables:

  1. Open the following files:
    • lt-06-01.p
    • lt-06-02.p
    • lt-06-03.p
  2. Choose Buffer List.
  3. Select lt-06-01.p from the Buffer List and choose OK.
  4. Choose Compile Run. The following display appears:
  5. The code in procedure lt-06-03.p creates this display, but the value “Paris” originates in the main procedure lt-06-01.p, passes through lt-06-02.p, and arrives safely for use in the display.

  6. Choose Exit, then press SPACEBAR to return to the Procedure Editor.

Here is the code for the three procedures that create the display:

lt-06-01.p
/*1*/  DEFINE NEW SHARED VARIABLE Boston AS CHARACTER 
        INITIAL "Arrived Safely!". 
/*2*/  RUN lt-06-02.p 

lt-06-02.p
/*3*/   DEFINE SHARED VARIABLE Boston AS CHARACTER. 
/*4*/   DEFINE VARIABLE Londton AS CHARACTER. 
        ASSIGN London = Boston. 
/*5*/   RUN lt-06-03.p (INPUT London). 

lt-06-03.p
/*6*/  DEFINE INPUT PARAMETER Paris AS CHARACTER FORMAT "x(20)".
      DEFINE BUTTON btn-Exit LABEL "Exit". 
      DEFINE FRAME Frame1 
          SKIP(1)
          Paris SKIP(1)
          btn-Exit
              WITH SIDE-LABELS NO-BOX CENTERED THREE-D.
        
/*7*/  DISPLAY Paris WITH FRAME Frame1.
      ENABLE ALL WITH FRAME Frame1.
      WAIT-FOR CHOOSE OF btn-Exit. 

NOTE: The THREE-D option is relevant only on a Windows client; it is ignored by a character client.

These notes explain the code and trace the value through the three procedures:

  1. The main procedure creates a new shared variable and gives it an initial value.
  2. The main procedure calls the second procedure. Note that the RUN statement uses no parameters.
  3. For this procedure to access the shared variable, it too must define it.
  4. This local variable passes on the value as a parameter. It is not necessary, because a shared variable can also be a parameter. The point here is to show how to pass the value of a local variable.
  5. This RUN statement calls the next procedure and specifies the local variable London as a parameter. Note the parentheses.
  6. The final procedure defines one parameter. During run time, Progress checks to make sure that both the calling and called procedures specify the same number of parameters.
  7. Because of the defined parameter, you can use Paris as a valid reference anywhere you can use a variable name.

Copyright © 2004 Progress Software Corporation
www.progress.com
Voice: (781) 280-4000
Fax: (781) 280-4095