Hard code or portable code

J. Landman Gay jacque at hyperactivesw.com
Thu Mar 11 01:31:55 EST 2004


On Wednesday, March 10, 2004, at 04:09 PM, hershrev wrote:
>
> Can I please have a little understanding on the parameter part of 
> functions what does it do or add?

The following is kind of long, so apologies in advance. I wrote it for 
someone else a long time ago. The person I was writing to said he never 
used custom functions because he didn't know what they were. He asked me 
to explain.

***

If Revolution does not have a built-in function to do something you 
want, you can write your own function to do it instead. Suppose we want 
to add two numbers together and get a total. (This is a very simple 
example, and really you would not need to do this because MC can add two 
numbers easily inside a script. But it makes a good example.) So we can 
write 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 to the function. In this case, they 
each will contain a number. The special word "return" tells Revolution 
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. 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 
"num1" and "num2", then add them together, and send back "20". The 
script "myHandler" will receive that 20 and put it into the variable 
"myTotal". So myTotal now equals 20.

When you use a function in a handler, you must provide a place for its 
results to go -- a variable usually, or sometimes a field. For example, 
this will not work:

   addNumbers(theFirstNumber,theSecondNumber)

because there is no place for the returned information to go. A handler 
that uses a function must provide a place to store the information that 
the function sends back. This is true of built-in Rev functions too. For 
example, this will not work:

   the date

because there is no place for the date to be stored. You must provide a 
place:

   put the date into myDate

A handler that uses a custom function must follow the same rule. It must 
provide a place for the returned information to be stored:

   put addNumbers(theFirstNumber,theSecondNumber) into myTotal

Functions can be as short or as long as you want, and sometimes can be 
very complicated. But the basics are always the same: a handler asks a 
function to do some work and often sends some parameters to the function 
to tell it what to use for that work. The function does the work and 
returns the finished calculation to the original handler. The handler 
can use the finished calculation any way it wants, just as if it were 
any other variable.

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


More information about the use-livecode mailing list