| Þ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
We took PROC001 and changed only one line (we have coloured it differently, as it might be difficult to see). To make a parameter in-out, in the calling module simply precede the variable name with the period. The output you get when you run PROC005 is
Before calling procedure PROC, the value of VARIABLE in the calling module is 10
|
After calling procedure PROC, the value of VARIABLE in the calling module is 11
Everything else remains the same.
Different programming languages link the variable and parameter in an in-out scenario differently. Let us analyze how M does it.
PROC006Þ;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(PARAM)ÞD
SETBGCOL^COLLIB("YELLOW")
ÞW !!!,?10,"Just entered
procedure PROC. Value of PARAM is ",PARAM
ÞW !,?5,"(Value of
VARIABLE in main is ",VARIABLE,")"
P10ÞS PARAM=PARAM+1
ÞW !!,?10,"After adding 1
to PARAM in procedure PROC, value of PARAM is
",PARAM
ÞW !,?5,"(Value of
VARIABLE in main is ",VARIABLE,")"
ÞD
SETBGCOL^COLLIB("BLACK")
ÞQ
Weve modified the procedure PROC in the program PROC005 slightly to report the value of VARIABLE in the calling module. In order to "see" the value of VARIABLE, weve had to change the name of the parameter. Within PROC we report the value of VARIABLE. As soon as PARAM is incremented, VARIABLE is incremented as well. This means that the variables are linked (in actual fact, the procedure parameter points to the memory location holding the passed variable).
Before we conclude on this topic, there is one thing you should remember with in-out parameter passing; you can only perform this type of operation with variables and will get an error if you attempt this with literals (string or numeric). Why?
Modify PROC003 so that
function HOWMANY is changed from a function to a
procedure with in-out parameter passing.
You are writing a function that regulates the temperature inside a compartment within a liquid cooled supercomputer. Your function will control a coolant release valve through an in-out parameter. The formula for calculating the amount of coolant is V = 10 * t / 10000, where V is the valve setting and t is the temperature. Your function must work out V and return it back to the calling module within the second parameter of the function. It must also return the string "Red: Shutdown" if t > 1000, "Yellow: Switch some of the CPUs off" if t > 750. All lesser values of t should cause the function to return the string "Green: Temperature OK". This is to be done via the QUIT command.
Goodbye
Next time we will start delving into intrinsic function, that is those functions that are standard in M. Each M implementation has a number of functions that provide quite a lot of functionality. We have already had to use some functions in the past, but now we will go through them in a proper and thorough manner.
Before we signoff we would like to re-remind you that in programming practice is an essential factor to mastering the syntax of any language. So get those fingers moving. If you have any code of your own you would like to share with others, please send it over to us at [email protected]. We will print any code we think is appropriate.
Boolean Variables Variables in M are untyped meaning that what can be stored within them does not have to be defined before. A program may set the variable A=10 meaning that A is storing a number after which the contents of A are changed to "Hello World" (a string). In this section we are going to talk a bit about Boolean variables. These variables are named after the mathematician George Boole. This man was fascinated by the two numbers 0 and 1 (and as some texts claim) in these numbers he saw the absence and presence of God). A Boolean variable is a switch variable. If we place a 1 in it we mean that the variable is "on". A 0 would be equivalent to "off". When using properly named variables, Boolean variables can make your programs easy to understand and maintain. This little game (Lander) should help you understand how you can use Boolean Variables effectively. BOOL001Þ;Boolean Variables - ACB - Feb
1997 |
|
Showcase your work here If you
would like to send in your M-related code, simply
attach a description to it, zip it up and send it
to us. The description should list the files in
the archive, installation notes, what the program
does and copyright information. Please clarify if
your submission is shareware / trialware /
freeware and other information you see |

![]()