View Single Post
Old 08 July 2015, 16:33   #42
BigFan
Registered User
 
BigFan's Avatar
 
Join Date: Feb 2014
Location: Germany
Posts: 261
Modularity and functions

Modularity

Do you remember me writing about file endings? .rexx in particular?

ARexx searches for files with .rexx first in current directory then in logical
drive REXX:. If the file ending is omitted, the file name matches both variants
e.g. file name is "wordcount.rexx", searching for "wordcount" matches as well as
"wordcount.rexx".
How is this related to modularity?
Imagine you wrote lots of small scripts for different purposes like :
word filter, word count, backup strategy, ports comunication, helper process.
All your scripts are named <filename>.rexx and reside in REXX:. Now you can use
them as command/function in your newly written code by typing their filename
without file ending. Lets assume a script called welcome.rexx is in REXX:
Code:
/**/
...
your code
welcome
...
To make the most of it your code has to be aware of arguments and parameters
passed to your external script as well as returning values from scripts.

ARexx features following methods to interact with external code:

Parsing arguments, using command "Arg" or "Parse" and check their existence with
"Arg()"

Returning values with command "Return"

Copy to Clipboard. The clipboard is available globally for all rexx programs.

Rexxsupport.library provides methods to open a rexx message port . It allows you
to send commands to a rexx program using "Address <portname> <command>". You have
to run this second program first to be up before the caller.
Use "WaitforPort" inside caller to avoid timing problems.

This lib offers the functions "AllocMem()/FreeMem()". They look similar to Getspace()/
Freespace, but memory allocated this way is public, while Getspace() takes from REXX
internal memory pools, which is private. Data stored in allocated memory can be shared
with other programs and is not freed automatically on exit. Be careful to not waste mem.

Modularity is not restricted to external code. The larger the script grows in
complexity, the higher the risk of failure and the less the readability. In short
your code becomes weird. This is called "spaghetti code".
A simple and recommended programming style is the use of own funtions.

Functions

To create your own function place a label. A label is a symbol with a colon.
Set command "Procedure" after your label. This can be placed anywhere in your
function but only once.
End function with "Return" e.g.

Code:
/**/
...
CheckForPort: Procedure 
... 
your code here
...
Return msg
To run code from own functions simply call them using command "Call" when needed.
The interpreter then stops execution at current line, jumps to the label, executes
the code and returns back to the next statement right after the call.

In very small scripts this would create some overhead, but in scripts with repetive
code sections this will shrink the code and structures it for ease of maintenance.

Functions have some advantages:

Protection of symbols
Your symbols outside your function are save from being overwritten. This feature
is on by default. It can be turned on or off for each function block, e.g.
Code:
/**/
...
CheckForPort: Procedure Expose i j
Now the variables i and j used by the caller will be overwritten with values
from your function. Expose has a left to right read order. Stems are translated
only as leftmost argument or as sole argument (msg.a becomes msg.21 if a = 21).
With " Expose A msg.A ", A becomes 21 while msg.A remains msg.A.
Compounds can be exposed
all at once using their stem (CUBE. addresses CUBE.X, CUBE.X.Y, CUBE.X.Y.Z etc).

Argument parsing
upto 15 arguments can be passed, parsed and assigned to variables for functions.
Commands use 1 Argument passed to them as a string. Parsing of a string is not
restricted by the means of length or number of substrings.

Recursion
a function can call itself. this results in very dense code. think of factorials.

Last edited by BigFan; 12 July 2015 at 10:04.
BigFan is offline  
 
Page generated in 0.04904 seconds with 11 queries