IBM Mainframes PLI Training Class7 Subroutines Introduction A
IBM Mainframes PL/I Training Class-7
Subroutines Introduction § A subroutine is an internal or external procedure that is invoked by a CALL statement. § The arguments of the CALL statement are associated with the parameters of the invoked procedure. The subroutine is activated, and execution begins. The arguments (zero or more) can be input only, output only, or both. § A subroutine is normally terminated by the RETURN or the END statement. Control is then returned to the invoking block.
Subroutines § Introduction A subroutine procedure must: § Not have the RETURNS option on the procedure statement § Not be declared as an entry with the RETURNS attribute if it is an external procedure § Be invoked using the CALL statement, not a function reference § Not return a result value using the RETURN statement
Subroutines Example EMPPGM: PROCEDURE OPTIONS(MAIN); DCL 1 EMPREC, 3 EMPID FIXED BINARY(15) INIT(1111), 3 EMPNAME CHAR(10) INIT('ANIL KUMAR'), 3 EMPLOC, 5 CITY CHAR(10) INIT(' '), 5 STATE CHAR(10) INIT(' '), 5 PINCODE CHAR(10) INIT(' '), 3 EMPSAL FIXED DECIMAL(7, 2) INIT(12345); CALL GETLIST; CALL PUTLIST; GETLIST: PROC; GET LIST(EMPID, EMPNAME, STATE, CITY, PINCODE, EMPSAL); END GETLIST; PUTLIST: PROC; PUT LIST(EMPID, EMPNAME, STATE, CITY, PINCODE, EMPSAL); END PUTLIST; END EMPPGM;
Subroutines Example PRMAIN: PROCEDURE; DECLARE NAME CHARACTER (20), ITEM BIT(5), OUTSUB ENTRY; CALL OUTSUB (NAME, ITEM); END PRMAIN; OUTSUB: PROCEDURE (A, B); DECLARE A CHARACTER (20), B BIT(5); PUT LIST (A, B); END OUTSUB; § The CALL statement in PRMAIN invokes the procedure OUTSUB in with the arguments Name and Item. § OUTSUB associates Name and Item passed from PRMAIN with its parameters, A and B.
Functions Introduction § A function is a procedure that has zero or more arguments and is invoked by a function reference in an expression. The function reference transfers control to a function procedure; § The function procedure returns control and a value, which replaces the function reference in the evaluation of the expression. The evaluation of the expression then continues. § A function procedure must: § Have the RETURNS option on the procedure statement. § Be declared as an entry with the RETURNS attribute, if it is an external procedure.
Functions Introduction § Be invoked using a function reference. The CALL statement can be used to invoke it only if the returned value has the OPTIONAL attribute. In this case, the returned value is discarded upon return. Using END instead of RETURN can cause unpredictable results. § Have matching attributes in the RETURNS option and in the RETURNS attribute. § Use the RETURN statement to return control and the result value. Whenever a function is invoked, the arguments in the invoking expression are associated with the parameters of the entry point. Control is then passed to that entry point. The function is activated and execution begins. The RETURN statement terminates a function and returns the value specified in its expression to the invoking expression.
Functions Example MAINP: PROCEDURE; GET LIST (A, B, C, Y); X = Y*3+SPROD(A, B, C); SPROD: PROCEDURE (U, V, W) RETURNS (BIN FLOAT(21)); DCL (U, V, W) BIN FLOAT(53); RETURN (U*V*W); END SPROD; When SPROD is invoked, the arguments A, B, and C are associated with the parameters U, V, and W in 2, respectively. SPROD is a function because RETURNS appears in the procedure statement. It is internal, and therefore needs no explicit entry declaration. If SPROD were external, MAINP would contain an entry declaration with RETURNS specified. SPROD returns the value represented by U*V*W, along with control to the expression in MAINP. The returned value is taken as the value of the function reference, and evaluation of the expression continues.
Thank You
- Slides: 9