Page 1 of 1

[SOLVED] Functions, commands and "on"

Posted: Thu Apr 23, 2020 3:42 am
by anmldr
This has been asked and answered before by others. I 'think' that I understand the difference between commands and functions.

If I am correct, a command just acts on something. A command is given and is then followed. A function may do something too but it then actually returns a value. But, I am still not clear about the "on" keyword as in:

Code: Select all

on mouseUp
//do something here
end mouseUp
Is "on" a command, a function or something else? Is it just used with messages such as being used before a "mouseUp" message?

Somewhere, I saw code that used all caps for "COMMAND". I don't understand that unless it is just a personal preference. Is that correct? In another language that I used, many years ago, all caps were used for constants.

Thank you,
Linda

Re: Functions, commands and "on"

Posted: Thu Apr 23, 2020 8:59 am
by Klaus
Hi Linda,
Is "on" a command, a function or something else?
ON is a command, a function starts with erm. FUNCTION! :-D

A FUNCTION is some kind of "helper" which does some computing (or whatever) for you and can deliver the result:

Code: Select all

on mouseup
  answer do_multiplication(2,99)
end mouseup

## Cheesy example, but you should get the picture :-)
function do_multiplication tParam1,tParam2
   return tParam1 * tParam2
end do_multiplication
FUNCTIONS can do more than that, but this is a general description.

ON is used to script the "reaction" on build-in events like mouseu, opencard etc.
Some years ago the mothership advised us to use COMMAND when creating custom handlers
and leave ON for the build-in commands because the engine would differ this later somehow.
But that did not happen so far.

Some developers however took this advise, like me, and use command for custom handlers:

Code: Select all

command my_custom_handler
  ## do this
  ## do that
  ## and that one, too
end my_custom_handler
Hope that helps!


Best

Klaus

Re: Functions, commands and "on"

Posted: Thu Apr 23, 2020 9:20 am
by bogs
Heh, well, I can't speak for Linda of course, but it helped raise my understanding of it, specially since I was not around when...
Klaus wrote:
Thu Apr 23, 2020 8:59 am
...years ago the mothership advised us to use COMMAND when creating custom handlers
and leave ON for the build-in commands...
...which actually makes sense now.

I do have to admit, unlike most languages I've used in the past, other than for clarity I've never seen the purpose behind the separation of the way handlers were written, even though I've seen other explanations previously.

Very interesting!

Re: Functions, commands and "on"

Posted: Thu Apr 23, 2020 12:23 pm
by SparkOut
You can also specify a "private command" but not afaik a "private on"

Re: Functions, commands and "on"

Posted: Thu Apr 23, 2020 12:28 pm
by Klaus
Exactly, since all build-in commands (ha! :-) ) are "public" in that sense.

Re: Functions, commands and "on"

Posted: Thu Apr 23, 2020 3:49 pm
by dunbarx
In essence, it is a matter of usage and convenience. Try this. Make a button and a field. Put this into the button script:

Code: Select all

on mouseUp
   put "" into fld 1
   wait 20 --just for effect
   
    doubleValue 42 --A command, which loads a value into the field
    
    put timesTwo(42) into item 2 of fld 1 --A function, which appends a second value 
    
    anotherTimesTwo 42 --using a command as a function
    put the result into item 3 of fld 1 --appends a third value
end mouseUp

on doubleValue var
   put var * 2 into fld 1
end doubleValue

function timesTwo var
   return var * 2
end timesTwo

on anotherTimesTwo var
   return var * 2 
end anotherTimesTwo
It is possible, though inconvenient, to not use functions at all. The command handler "anotherTimesTwo" substitutes itself for a function, using "the result" as the vehicle for returning a value to the calling handler. That is the beauty and compactness of functions, though. They do all that in a very readable and accessible form. I would stick with the basics. Knowing the nuanced differences is useful. Having them at our disposal is priceless.

Craig

[SOLVED] Functions, commands and "on"

Posted: Fri Apr 24, 2020 5:52 am
by anmldr
Excellent answers from all. Thank you.