in any programming language, the entire program is broken into logical modules. this makes it easier to write code that can be maintained easily. this is a basic requirement for any programming language.
in rexx, modules can be written using subroutines and functions. let’s look at the subroutines in detail.
defining a subroutine
the syntax of a function declaration is as follows −
functionname: statement#1 statement#2 …. statement#n
where,
functionname − this is the name assigned to the subroutine.
statement#1 .. statement#n − these are the list of statements that make up the subroutine.
the following program is a simple example showing the use of subroutines.
/* main program */ call add exit add: a = 5 b = 10 c = a + b say c
the following things should be noted about the above program −
we are defining a subroutine called add.
the subroutine does a simple add functionality.
the exit statement has to be used to signify the end of the main program.
the output of the above program would be as follows −
15
working with arguments
it is also possible to work with arguments in rexx. the following example shows how this can be achieved.
/* main program */ call add 1,2 exit add: parse arg a,b c = a + b say c
the following things should be noted about the above program −
we are defining a subroutine called add which takes on 2 parameters.
in the subroutines, the 2 parameters are parsed using the parse and arg keyword.
the output of the above program would be as follows −
3
different methods for arguments
let’s look at some other methods available for arguments.
arg
this method is used to return the number of arguments defined for the subroutine.
syntax −
arg()
parameters − none
return value − this method returns the number of arguments defined for the subroutine.
example −
/* main program */ call add 1,2 exit add: parse arg a,b say arg() c = a + b say c
when we run the above program we will get the following result.
2 3
arg(index)
this method is used to return the value of the argument at the specific position.
syntax −
arg(index)
parameters
index − index position of the argument to be returned.
return value − this method returns the value of the argument at the specific position.
example −
/* main program */ call add 1,2 exit add: parse arg a,b say arg(1) c = a + b say c
when we run the above program we will get the following result.
1 3