Learning Functions

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
gpearson
Posts: 84
Joined: Wed Feb 03, 2010 12:55 pm

Learning Functions

Post by gpearson » Sat Feb 27, 2010 4:02 am

As I am learning RR I have created my first application which has given me many ideas for additional desktop applications. The following code I have used within an on MouseUp code block and I am wanting to create its own function.

Code: Select all

   put "" into tString
   put "User-Agent: WeAreClosedToday DesktopWeatherApplication/" into tUserAgent
   put "1.0.0" after tUserAgent
   set the httpHeaders to tUserAgent
   
   put url ("http://demo.weareclosedtoday.com/properties/xmlfeed/runrevfunctions.cfm?Method=QueryWUndergroundStations&ZipCode=" & passZipCode) into tWUndergroundStationsData
   put revCreateXMLTree(tWUndergroundStationsData, false, true, false) into tWUndergroundStationsTreeID
   
   if tWUndergroundStationsTreeID is not a number then 
      answer error "XML Tree not Created due to error"
      exit GetWeatherInfo
   end if
   
   put revXMLRootNode(tWUndergroundStationsTreeID) into tWUndergroundStationsXMLRootNode
   put revXMLNumberOfChildren(tWUndergroundStationsTreeID, tWUndergroundStationsXMLRootNode, "", 0) into tWUndergroundStationsXMLChildrenNodes
   put revXMLNodeContents(tWUndergroundStationsTreeID, "RunRevXML/ClosestAirportCode") into tClosestAirportCode
   put revXMLNodeContents(tWUndergroundStationsTreeID, "RunRevXML/ClosestPersonalWSCode") into tClosestPersonalWSCode
   
   put url ("http://demo.weareclosedtoday.com/properties/xmlfeed/runrevfunctions.cfm?Method=QueryWUndergroundAirportStatus&AirportCode=" & tClosestAirportCode) into tWUndergroundAirportData
   put revCreateXMLTree(tWUndergroundAirportData, false, true, false) into tWUndergroundAirportTreeID
   
   if tWUndergroundAirportTreeID is not a number then 
      answer error "XML Tree not Created due to error"
      exit GetWeatherInfo
   end if
   
   -- Put Information into Variables to be displayed on the screen within the Stack
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/ObservationTime") into tObservationTimeField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/Weather") into tWeatherField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/RelativeHumidity") into tRelativeHumidityField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/Temperature") into tTemperatureField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/WindString") into tWindSpeedField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/PressureString") into tPressureStringField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/DewPointString") into tDewPointStringField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/HeatIndexString") into tHeatIndexStringField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/WindChillString") into tWindChillStringField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/VisibilityMi") into tVisibilityMiField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/IconBaseURL") into tIconBaseURLField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/IconBaseName") into tIconBaseNameField
   put revXMLNodeContents(tWUndergroundAirportTreeID, "RunRevXML/CurrentIcon") into tCurrentIconField
   
   -- Lets Display the information to the Card
   put revXMLNodeContents(tWUndergroundStationsTreeID, "RunRevXML/LocationCountry") into field "CountryField" on card "Get Weather Info"
   put revXMLNodeContents(tWUndergroundStationsTreeID, "RunRevXML/LocationState") into field "StateField" on card "Get Weather Info"
   put revXMLNodeContents(tWUndergroundStationsTreeID, "RunRevXML/LocationCity") into field "CityField" on card "Get Weather Info"
   set the filename of image "CurrentWeatherImage" on card "Get Weather Info" to tIconBaseURLField & tCurrentIconField & tIconBaseNameField
   put tObservationTimeField into field "ObservationTimeField" on card "Get Weather Info"
   put tTemperatureField into field "Current Temp Field" on card "Get Weather Info"
   put tDewPointStringField into field "Dew Point Field" on card "Get Weather Info"
   put tWindSpeedField into field "Wind Speed Field" on card "Get Weather Info"
   put tRelativeHumidityField into field "Relative Humidity Field" on card "Get Weather Info"
   put tPressureStringField into field "Pressure Field" on card "Get Weather Info"
   put tVisibilityMiField & " Mile(s)" into field "Visibility Field" on card "Get Weather Info"
   
   put "" into field "Feels Like Label" on card "Get Weather Info"; put "" into field "Feels Like Field" on card "Get Weather Info"
   if "NA" is not in tHeatIndexStringField then
      put "Heat Index: " into field "Feels Like Label" on card "Get Weather Info"
      put tHeatIndexStringField into field "Feels Like Field" on card "Get Weather Info"
   else if "NA" is not in tWindChillStringField then
      put "Wind Chill: " into field "Feels Like Label" on card "Get Weather Info"
      put tWindChillStringField into field "Feels Like Field" on card "Get Weather Info"
   end if
If I wrap the above code with

Code: Select all

function GetWeatherInfo passZipCode

end GetWeatherInfo
and put it on the Main Stack Script I have tried on the card script to call GetWeatherInfo(46528) which upon running the stack, I get an error message of

Unable to find Handler


Anyone have any ideas?
---
Graham Pearson
Goshen, IN USA

We Are Closed Today is your single internet resource for Indiana Area School Closings, Visit http://www.weareclosedtoday.com

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Re: Learning Functions

Post by BvG » Sat Feb 27, 2010 5:38 pm

there's two different type of handlers:

a function looks like this:

Code: Select all

function GetWeatherInfo theValue
  return theValue + theValue
end GetWeatherInfo
a command looks like this:

Code: Select all

function GetWeatherInfo theValue
  put theValue + theValue into field "a field"
end GetWeatherInfo
these should work if the function or command is in the message path (this is the case for you as you said it's in the mainstack):

the function which is called by doing:

Code: Select all

put GetWeatherInfo(46528) into myValue
or the command which works like this:

Code: Select all

GetWeatherInfo "46528"
You only need to use "call", "send" or "dispatch" when the function or command is not in the message path.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

Regulae
Posts: 136
Joined: Tue Oct 20, 2009 6:05 am

Re: Learning Functions

Post by Regulae » Sun Feb 28, 2010 1:42 am

I think there’s a typographical slip above:

a command looks like this:

Code: Select all

function GetWeatherInfo theValue
  put theValue + theValue into field "a field"
end GetWeatherInfo
is, I believe, intended to be:

a command looks like this:

Code: Select all

on GetWeatherInfo theValue
  put theValue + theValue into field "a field"
end GetWeatherInfo
Looking at the code block you want to put in your stack script, it seems you want it to execute a series of instructions, rather than compute and return a value. If you were to wrap the block in the stack script with:

Code: Select all

on GetWeatherInfo passZipCode

end GetWeatherInfo
... you can call this command by:
GetWeatherInfo 46528
or:
GetWeatherInfo “46528”
say, typed into the message box, or in the mouseUp of a button, whatever. You can pass a parameter, like your zip code, to be used by a command or a function, but with a function the parameter must be enclosed in brackets, e.g. the function doubleTheNumber(5) which, from its name, would presumably return the value (10). The Rev User Guide has an extensive discussion of commands, functions and parameters, which to be honest I’m still coming to grips with.

Post Reply