Teach You, Teach Me (Part 2)
Library routines
Recall that in MWM001 we referred to the Math library. If we wanted to work out the square root of a program all we had to do was to tell it that we wanted the routine SQR from the routine %MATH.
Copy SHOWMSG and HELPLINE into two independent modules and remove them from RTN003. Thus you should have RTN003, SHOWMSG and HELPLINE. Alter RTN003 so that whenever a call is made to SHOWMSG and HELPLINE, instead of looking for them within RTN003 they will be search for within your database. (Hint: Precede the routine names with ^ to let the M interpreter look up within the database).
In the Try It problem above, you were asked to produce two routines. Since in M, routines tend to be very small, this would result in a large number of programs making it rather difficult to trace a not-so-frequently-used module. It is sometimes more convenient to group together a number of similar routines in one unit. Rather than have many small stand alone programs we place them in one shell. To specify one particular routine within one shell we specify <procedure>^<routine> for example SHOWMSG^IOLIB (i.e. the routine SHOWMSG from the library IOLIB.
RTN004Þ
;Program demonstrates procedure calls - May 1997
Þ;M Web Magazine @
http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
ÞD GETNME
ÞD GETAGE
ÞD SHOWMSG^IOLIB("Thank You")
ÞQ
Þ;*** EOR ***
GETNMEÞ
;Procedure asks user to enter member's name and checks for a NULL
(emtpy) string
ÞW # D HELPLINE^IOLIB("Enter Member's
Name and Surname")
ÞW /CUP(2,5),"Details:" R
/CUP(2,30),WHO ;Get Actual Details
ÞW /CUP(24,1),/EL(2) ;Clear Line 24
ÞI WHO="" D SHOWMSG^IOLIB("This
field cannot be left blank") G GETNME
ÞQ
Þ;*** EOR ***
GETAGEÞ;Member
Age must be below 18
ÞD HELPLINE^IOLIB("Enter age as at last
birthday")
ÞW /CUP(4,5),/EL(0),"Age:"
ÞR /CUP(4,30),AGE
ÞW /CUP(24,1),/EL(2)
ÞI AGE>17 D SHOWMSG^IOLIB("This person
is too old") G GETAGE
ÞQ
IOLIBÞ;Input
/ Output Library Routines - May 1997
Þ;M Web Magazine @
http://geocities.datacellar.net/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
SHOWMSG(MESSAGE)Þ
S SPOS=(80-$L(MESSAGE))\2
ÞW /BEL,/CUP(23,SPOS),MESSAGE
ÞR *ERR
ÞW /CUP(23,1),/EL(2)
ÞQ
Þ;*** EOR ***
HELPLINE(MESSAGE)Þ
;Animates the Line Display
ÞF I=1:1:79 W
/CUP(24,79-I),/EL(2),$E(MESSAGE,1,I) H:I#40=0 1
ÞQ
A Few Questions
All said, our reviewers still had the following questions:
Q1. What is the difference between a DO and a GO command?
A1. Both GO and DO transfer program execution to the label specified after the command. The GO command effects an unconditional jump. This means that the program does not need to know from where it left. DO must "remember" where it left off because, upon encountering a QUIT in the called module, control must return to the calling module. This remembering is achieved thanks to a structure called a stack.
Q2. There are two variety of DO's: DO ^<name> and DO <name>. What is the difference?
A2. The circumflex ^ simply tells us if the routine <name> is sited within the same program or if it must be loaded from disk. For example when we run a program from the command prompt we have to specifically tell our M implementation that this program must be loaded from disk. Without the ^ we know that the routine is already loaded as was the case with the examples we demonstrated above. Therefore we can extend this to cover a situation where a program calls a block not within it, but situated on disk. This routine can thus be available for all programs that would like to call it.
Q3. QUIT is used in a lot of places (FOR, ending the program, blocks). Is there a way of distinguishing between them?
A3. Think of QUIT as a terminating command. It stops a process. With the FOR you are dealing with an iteration (looping situation) which must come to an end if the program is to continue. QUIT does that job. When we think about program termination or block termination, the idea is quite similar and if you think of the command prompt as a continually running program no different from a one of your own routines you'll notice no difference.
Q4. When grouping together routines in a library, wouldnt this be inefficient?
A4. Yes, but when one weighs the benefit of having a library grouped together it does normally outweigh this cost. A library routine that is invoked many times will ultimately end up being cached meaning that there will be little overheads in seeking the routine from hard disk. This is similar to inline documentation: taking up disk space without constituting an executable command.
Who's Next |
---|
We would like to meet the people who made, or are making M the great
language that it is. If you are that person or know of someone who qualifies for this page, let us know. So that mail intended for this page will be handled correctly, please set the subject line to MWM Who. |
Flowcharts
Flowcharts are a very popular topic amongst our readers, and continuing with our trend, we are now listing the flowchart of the last program demonstrated above. There are two new symbols, the procedure symbol and the iterating symbol and the comment symbol.
If you look closely at our flowcharts, modularity allows us to zoom to great depths when we produce code in a modular manner, something not normally done when a single flowchart has to describe a lot of stuff.
The procedure symbol tells us that we are dealing with a high-level black box. We have, in a way, extended Ms keywords with out procedures and this box reflects just that. Each procedure box can, in turn, be converted into a flowchart.
The iterating symbol is used to describe a loop. Do not forget that flowcharts are language independent meaning that their main purpose is to describe how a program operates and not how to go about doing the task. Different programmers will code a different solution to the same flowchart though the overall result should be very similar. (This symbol is normally using in Nassi-Schniderman diagrams).
The comment symbol (rectangle with one side open) allows you to annotate comments with certain symbols.