MF 

Parameterized Sections (examples)

The following set of examples shows various combinations and methods of passing arguments to a parameterized section, and demonstrates the results returned.

Example 1

The following example shows a conventional perform of a parameterized section returning a value, and also a functional perform, where the parameterized section is executed to provide a resulting value to a local variable.

       declare i0 as binary-long
       perform m2(3) returning i0
       display "i0 = " i0
       declare i1 as binary-long = m2(4)
       display "i1 = " i1
       goback.
       m2 section (i1 as binary-long) returning ret as binary-long.
         compute ret = i1 + 1.

Running the code above gives the following output:

i0 = +0000000004
i1 = +0000000005  

Example 2

The following example shows a parameterized section without any parameters (and therefore must have a returning clause).

       declare i1 as binary-long = m1()
       display "i1 = " i1
       declare i2 as binary-long = m2(4)
       display "i2 = " i2
       goback.
       m1 section() returning ret as binary-long.
         move 103 to ret.
       m2 section(i1 as binary-long) returning ret as binary-long.
         add 1 to i1 giving ret.

Running the code above gives the following output:

i0 = +0000000103
i1 = +0000000005  

Example 3

The following example shows a parameterized section being used as an argument for another parameterized section.

       display a(b(3))
       goback.
       a section(i1 as binary-long) returning i2 as binary-long.
         compute i2 = i1 + 2.
       b section(i1 as binary-long) returning i2 as binary-long.
         compute i2 = i1 * 3.

Running the code above gives the following output:

+0000000011

Example 4

The following example shows a parameterized section with multiple parameters, and also demonstrates a number of combinations of how the arguments can be passed.

       display a(b(3), b(5))
       display a(b(3), c(5))
       display a(b(3), 7)
       display a(8, c(5))
       display a(8, 9 + c(5))
       display b(a(8,9 + c(5)))
       goback.
       a section(i1 as binary-long, i2 
                   as binary-long) returning ret as binary-long.
         compute ret = i1 + i2.
       b section(i1 as binary-long) returning i2 as binary-long.
         compute i2 = i1 * 3.
       c section(i1 as binary-long) returning i2 as binary-long.
         compute i2 = i1 * 4.

Running the code above gives the following output:

+0000000024
+0000000029
+0000000016
+0000000028
+0000000037
+0000000111

Example 5

The following example shows a parameterized section with an optional parameter. The two functional performs demonstrate when the optional value is ignored or used.

       declare i1 as binary-long = m2(4)
       display "i1 = " i1
       compute i1 = m2()
       display "i1 using optional param = " i1
       goback.
       m2 section(i1 as binary-long = 998) returning ret as binary-long.
         compute ret =i1 + 1.

Running the code above gives the following output:

i1 = +0000000005
i1 using optional param = +0000000999

Example 6

The following example shows the effects of passing a parameter by reference. (Note, when reference or value is not specified, the default is to pass the parameter by value. Also, if there is a mismatch in passing modes between the statement and the section, the mode specified in the section is used.)

       01 n1 binary-long value 2.
       perform a(reference n1)
       display "Value on exit = " n1
       goback.
       a section(reference i1 as binary-long).
         display "value on entry = " i1
         move 999 to i1.         

Running the code above gives the following output:

value on entry = +000000002
value on exit = +0000000999

Example 7

The following example shows the use of a typedef group as a parameter, and also using that parameter by reference.

       01 td typedef.
           03 pic x.
           03 n1 pic 9(4).
           03 pic x.
           03 x1 pic x(40).
       01 g1 td.
       move 1234 to g1::n1.
       display "value before = " g1::n1.
       perform a(reference g1).
       display "value on exit = " g1::n1.
       goback.
       a section (reference g2 as td).
         display "value on entry = " g2::n1
         move 999 to g2::n1.   

Running the code above gives the following output:

value before = 1234
value on entry = 1234
value on exit = 0999