Goofy question #7234

Tore Nilsen tore.nilsen at me.com
Mon Jul 10 04:29:27 EDT 2017


Here is what I teach my students:

Use the function to return a value to the calling handler, set the properties in the handler, not in the function itself, like this:

on mouseUp
 set backgroundColor of this card to goRed()
end mouseUp

function goRed
  return “red”
end goRed

The benefit of this is that you can call the function from various handlers and sending whatever parameter you like. Here is a script that returns different colours
depending on which card you open when you call the function. Everything is placed in the stack script:

on preOpenCard
 put short name of this card into tCard
 set backgroundColor of card tCard to setColour(tCard)
end preOpenCard

function setColour pCard
 if pCard = “One” then
   return “red”
 else
   return “blue”
 end if
end setColour


Regards
Tore Nilsen

> 10. jul. 2017 kl. 10:09 skrev Tim Selander via use-livecode <use-livecode at lists.runrev.com>:
> 
> Hi Richmond
> 
> This works (both scripts in the button):
> on mouseUp
> put goRed()
> end mouseUp
> 
> function goRed
> set the backGroundColor of card 1 to red
> end goRed
> 
> I think the general idea is that a function returns information. So date() returns the date. But just having a line "date()" in the script causes an error because you haven't told it what to do with the info being returned. So you need "put data()" and then the info will go to the message box.
> 
> Your function doesn't return any data. That's your choice when you write it. But the engine is still expecting a function to return info, so you still have to say "put" trigger the function.
> 
> You could write your function with a parameter:
> function goRed foo
> set the backGroundColor of foo to red
> end goRed
> 
> Then call the function like this:
> put goRed("card 1")
> put goRed("card 3")
> etc., and the one function can be used to turn both card 1 and card 3 (and any other object you specify in foo) red.
> 
> Hope that helps.
> 
> Tim Selander
> Tokyo, Japan
> 
> 
> 
> 
> 
> On 7/10/17, 15:48, Richmond Mathewson via use-livecode wrote:
>> I am obviously missing something . . .
>> 
>> . . . so badly so that I've been trawling Danny Goodman's "Complete" HC 2 . . .
>> 
>> and NOT getting 'it' . . .
>> 
>> SO: one can set up a custom command:
>> 
>> on GoRed
>>  set the backGroundColor of card 1 to red
>> end GoRed
>> 
>> and one can call it:
>> 
>> on mouseUp
>>   do GoRed
>> end mouseUp
>> 
>> and one can set up a custom function:
>> 
>> function GoRed
>>  set the backGroundColor of card 1 to red
>> end function
>> 
>> Now: is the reason I cannot call that function because it takes NO parameters?
>> 
>> Richmond.
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>> 
> 
> 
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list