Page 1 of 1

Learning Functions

Posted: Sat Feb 27, 2010 4:02 am
by gpearson
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?

Re: Learning Functions

Posted: Sat Feb 27, 2010 5:38 pm
by BvG
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.

Re: Learning Functions

Posted: Sun Feb 28, 2010 1:42 am
by Regulae
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.