MainframeSupports
tip week 35/2015:

Twelve weeks ago I wrote about recursion in COBOL. Now it is time for PLI. PLI is a so-called procedural programming language with options for defining internal procedures using parameters and local variables. Therefore the possibility of making recursive calls is more straightforward than in COBOL. I have translated the example from the COBOL tip as directly as possible to PLI:

parsing: PROC(jclparm) OPTIONS(MAIN);
DCL jclparm CHAR(100) VAR;
DCL needlepos FIXED BIN(31);
DCL parmno FIXED BIN(15) INIT(0);
DCL pgmparm CHAR(100) VAR;
pgmparm = jclparm;
CALL pgmstart;
pgmstart: PROC;
  DCL localno FIXED BIN(15);
  parmno = parmno + 1;
  localno = parmno;
  IF length(pgmparm) > 0
  THEN DO;
    needlepos = index(pgmparm, ',');
    IF needlepos = 0
    THEN
      needlepos = length(pgmparm) + 1;
    PUT SKIP LIST('PARM-NO='!!parmno!!', LOCAL-NO='!!localno
                 !!', VALUE=<'!!substr(pgmparm,1,needlepos - 1)!!'>'
                 );
    IF needlepos <= length(pgmparm)
    THEN DO;
      pgmparm = substr(pgmparm, needlepos + 1);
      CALL pgmstart;
      PUT SKIP LIST('PARM-NO='!!parmno!!', LOCAL-NO='!!localno
                   !!' AFTER RECURSION'
                   );
    END;
  END;
END pgmstart;
END parsing;

Opposite COBOL all procedure calls in PLI has its own return address no matter how many times the same procedure is called. That is why you cannot get into the same kind of trouble with recursion as you can in COBOL. The PLI manual instructs you to use the option RECURSIVE on procedures that are called recursive, but it is not a requirement and normally you will not notice any difference. There are though special situations around transfer of parameters where the use of RECURSIVE makes a difference.

In the example above it will be far more elegant to use pgmparm as a parameter in the procedure pgmstart. The first call to pgmstart would be CALL pgmstart(jclparm) and the recursive call would be CALL pgmstart(substr(pgmparm, needlepos + 1)) and the statement pgmparm = substr(pgmparm, needlepos + 1) must be removed. In this way the procedural part of PLI is much better used. Please also remark that local variables in PLI are truly local variables and not as in COBOL global variables for the current execution of the program as a whole.

Previous tip in english        Forrige danske tip        Tip list