The long answer on when to use a function vs command

Ken Ray kray at sonsothunder.com
Tue Mar 14 23:46:14 EST 2006


On 3/14/06 10:31 PM, "Thomas McGrath III" <3mcgrath at adelphia.net> wrote:

> Hello listeroos,
> 
> Can someone give the long answer on when is it best to use a function
> versus a command? I have a lot to write of both commands and
> functions and have been overly confusing myself. Please give at least
> two examples each with parameters and how to call/use them.

In general, you use functions when you want to return some value, and you
use commands to "execute" things. However, both constructs allow you to do
both, so it's really a matter of preference.

An example of a typical function:

on mouseUp
  put addEmUp(5,10,20) into field 1
end mouseUp

function addEmUp pNum1,pNum2,pNum3
  put pNum1+pNum2+pNum3 into tTotal
  return tTotal
end addEmUp

and a typical command:

on mouseUp
  NotifyUser "We're done!"
end mouseUp

on NotifyUser pMsg
  put "Just wanted to tell you:" && pMsg into tText
  answer tText
end NotifyUser

However you could (if you wanted to), reverse them:

A function as a command:

on mouseUp
  addEmUp 5,10,20
  put the result into field 1
end mouseUp

on addEmUp pNum1,pNum2,pNum3
  put pNum1+pNum2+pNum3 into tTotal
  return tTotal
end addEmUp

A command as a function:

on mouseUp
  get NotifyUser("We're done!")
end mouseUp

function NotifyUser pMsg
  put "Just wanted to tell you:" && pMsg into tText
  answer tText
end NotifyUser

Your call as to how to use them... but in general, a function returns a
value and a command doesn't.

HTH,

Ken Ray
Sons of Thunder Software
Web site: http://www.sonsothunder.com/
Email: kray at sonsothunder.com




More information about the Use-livecode mailing list