M Tutorial

(Part 2)

By

Chris Bonnici

Parameters

Last time we introduced parameters and today we "go forth and conquer" more of this interesting topic. A parameter is a communication channel between the calling and the called modules. Through the parameter, the calling routine can pass values to the called module, which in turn will process these values. This makes the procedure or function more dynamic as it can handle different values.

PROC001Þ;Parameter Characteristics - ACB - July 1997
Þ;M Web Magazine @ http://www.geocities.com/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
Þ;*** main ***
ÞS VARIABLE=10
ÞW !!,"Before calling procedure PROC, the value of VARIABLE in the calling module is ",VARIABLE
ÞD PROC(VARIABLE)
ÞW !!!,"After calling procedure PROC, the value of VARIABLE in the calling module is ",VARIABLE
ÞQ
Þ;*** EOR ***
PROC(VARIABLE)
ÞD SETBGCOL^COLLIB("YELLOW")
ÞW !!!,?10,"Just entered procedure PROC. Value of VARIABLE is ",VARIABLE
ÞS VARIABLE=VARIABLE+1
Þ W !!,?10,"After adding 1 to VARIABLE in procedure PROC, value of VARIABLE is ",VARIABLE
ÞD SETBGCOL^COLLIB("BLACK")
ÞQ

When we run this program the output on our screen is similar to the one below.


Before calling procedure PROC, the value of VARIABLE in the calling module is 10

  Just entered procedure PROC. Value of VARIABLE is 10
After adding 1 to VARIABLE in procedure PROC, value of VARIABLE is 11

After calling procedure PROC, the value of VARIABLE in the calling module is 10


The Yellow area defines the time the procedure PROC is active. The other area belongs to top module (main). Although we have used the same name variable name (VARIABLE) everywhere, the variable within PROC is not the same as the variable in main. The parameter defines a new instance of all parameters. Proof of this is that although we have set VARIABLE to 11 before we exited PROC, in main, VARIABLE still retained its original value of 10.

When a procedure (or function) is invoked, all parameters are first NEWed, then initialized with the value set by the calling procedure. Whatever happens to these variables within the procedure is not effecting similarly named variables in the calling module. Upon module termination, these variables simply cease to exits.

In PROC002 we simulate the above program so that you can get a better understanding of all this. One thing to note is that this code is not efficient as using parameters and should never be used instead of them. Run the program; it is self-explanatory.

PROC002Þ;Parameter Characteristics - ACB - July 1997
Þ;M Web Magazine @ http://www.geocities.com/SiliconValley/7041/mwm.html
Þ;You are using this program at your own risk
Þ;
Þ;*** main ***
ÞS VARIABLE=10
ÞW !!,"Before calling procedure PROC, the value of VARIABLE in the calling module is ",VARIABLE
ÞD PROC
ÞW !!!,"After calling procedure PROC, the value of VARIABLE in the calling module is ",VARIABLE
ÞQ
Þ;*** EOR ***
PROC
ÞD SETBGCOL^COLLIB("YELLOW")
ÞW !!!,?5,"(First we copy 'parameter' VARIABLE into the temporary local variable TEMP)"
ÞN TEMP
ÞS TEMP=VARIABLE
ÞW !,?5,"(We create a NEW instance of VARIABLE in PROC and copy TEMP into it)"
ÞN VARIABLE
ÞS VARIABLE=TEMP
ÞW !,?5,"(We KILL off TEMP. The parameter simulation is complete)"
ÞK TEMP
ÞW !!,?10,"Just entered procedure PROC. Value of VARIABLE is ",VARIABLE
ÞS VARIABLE=VARIABLE+1
ÞW !!,?10,"After adding 1 to VARIABLE in procedure PROC, value of VARIABLE is ",VARIABLE
ÞD SETBGCOL^COLLIB("BLACK")
ÞQ

This creation of a new variable should help us understand why it is possible to name the parameter of the module differently from the variable it is assigned to in the calling program.

Modify PROC001, giving the parameter of the procedure a different name. Check that everything works correctly. Don’t forget to change all reference to the parameter inside to the procedure to that new parameter name.

Assume you only change the name of the parameter in PROC001 but left the references inside the procedure PROC unchanged. What will happen?

Work is defined as the force multiplied by the distance moved in the direction of the force; Power is the work done divided by the time. Write a program that requests the force, distance and time. In two separate modules (functions or procedures) work out this object’s work and power.

The drawing below plots the life of the variable. One variable is declared within the body of the main routine, while the other is initiated within a parameter (using the NEW command).

Fill in the Survey

M Campaign

M is a great language, but did you know that M isn't listed in many electronic and paper media as it should. If you do not find a reference to a programming language called M or MUMPS do the following:

  1. Check if there are other languages such as BASIC, COBOL and FORTRAN, PASCAL (or Database products).

If one or more of these (or other programming) languages are listed send us an e-mail with as much of the publication details as possible.

Don't forget, M is a powerful, multi platform, multi-user, development language that can operate in both Character and Graphical interface environments.

Click here for more information

E&OE

Tell a Friend!

1