Example
we will create a function that will accept 2 integer values, multiply them and return the result in a variable.FUNCTION DEMO.FUNCTION(ARG.1,ARG.2) RET.VALUE = ARG.1 * ARG.2 RETURN(RET.VALUE) ENDThe RETURN statement is used to return a value in a function.
Now the function defined above can be called from any program/subroutine. Now create a subroutine that would supply values and call the above-defined function. When calling a function, the function needs to be defined first using the DEFFUN command and only then the function should be called.
SUBROUTINE DEMO.SUB.CALLING.RTN VAR.1 = 10 VAR.2 = 20 DEFFUN DEMO.FUNCTION(VAL.1,VAL.2) VAR.3 = DEMO.FUNCTION(VAR.1,VAR.2) CRT "Result : "VAR.3 RETURN ENDIncase of a function, only the incoming parameters need to be defined. There can and will be only one return value and that will directly get assigned to the variable on the right side of the equation as defined above.
Un-initialized variables VAL.1 and VAL.2 have been given to demonstrate that any variable can be given at the time of defining a function.
0 comments:
Post a Comment