Parameters [WAS: Main menu puzzle]

J. Landman Gay jacque at hyperactivesw.com
Mon Feb 20 21:14:34 EST 2006


Ken Apthorpe wrote:
> I second what Mark says about these sorts of explanations, particularly the
> "Assume nothing" and inclusion of (complete) example scripts for new users.

I found my old notes (from the HyperCard list, but Rev is the same.) It 
has a simple example. If I get time, I will combine this with my earlier 
response and put it on my web site with my other essays.

****
My favorite analogy is that parameters are baskets. This fits in well 
with the HyperCard concept that variables are "containers". Parameters 
are just another variable container that catch and hold whatever values 
are sent to them. Suppose we have a function called addNumbers like this:

function addNumbers num1,num2
     put num1 + num2 into theTotal
     return theTotal
end addNumbers

The parameters "num1" and "num2" are like baskets that hold whatever 
values the original handler sends. In this case, they each will contain 
a number. The special word "return" tells the function to send the 
variable "theTotal" back to the handler that asked for the information. 
So, this function takes two numbers that are passed to it in the 
parameters, adds them together, and sends back a total. Parameter 
"baskets" are always filled in first-to-last order; whatever number 
arrives first will be placed into the "num1" basket, and whatever number 
arrives second will be placed into "num2". It is important that the 
order of the values sent are the same as those expected by the receiving 
parameters.

Now we can write a handler that uses this function this way:

on myHandler
    put 16 into theFirstNumber
    put 4 into theSecondNumber
    put addNumbers(theFirstNumber,theSecondNumber) into myTotal
end myHandler

This handler will send "16" and "4" to our custom function "addNumbers". 
The addNumbers function will catch these two numbers in its parameters. 
Since 16 was sent first, it will be received first and placed into the 
"num1" basket. The second number, 4, will be placed into the "num2" 
basket. The function uses these parameters just as though a script had 
issued statements such as "put 4 into num2"; the parameter variables are 
automatically filled with the sent values. The function can use these 
variables just like any other local variable. This function will add 
them together and send back "20". The script "myHandler" will receive 
that 20 and put it into the variable "myTotal".

You can have as many parameters as you need. A general rule of thumb is 
that passing parameters is prefereable to using globals, except where 
many different handlers need to access a value repeatedly. In that case, 
a global may be a better solution because it is faster and more 
universally available. However, in the above example, it would be silly 
to make num1 and num2 into globals; their values will be different each 
time the function is called, and it is trivial to pass the values to the 
function as needed.


-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com



More information about the use-livecode mailing list