[SOLVED] Functions, commands and "on"

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

[SOLVED] Functions, commands and "on"

Post by anmldr » Thu Apr 23, 2020 3:42 am

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
Last edited by anmldr on Fri Apr 24, 2020 5:52 am, edited 1 time in total.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Functions, commands and "on"

Post by Klaus » Thu Apr 23, 2020 8:59 am

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

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: Functions, commands and "on"

Post by bogs » Thu Apr 23, 2020 9:20 am

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!
Image

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: Functions, commands and "on"

Post by SparkOut » Thu Apr 23, 2020 12:23 pm

You can also specify a "private command" but not afaik a "private on"

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Functions, commands and "on"

Post by Klaus » Thu Apr 23, 2020 12:28 pm

Exactly, since all build-in commands (ha! :-) ) are "public" in that sense.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Functions, commands and "on"

Post by dunbarx » Thu Apr 23, 2020 3:49 pm

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
Last edited by dunbarx on Sat Apr 25, 2020 5:02 pm, edited 1 time in total.

anmldr
Posts: 459
Joined: Tue Sep 11, 2012 11:13 pm

[SOLVED] Functions, commands and "on"

Post by anmldr » Fri Apr 24, 2020 5:52 am

Excellent answers from all. Thank you.

Post Reply