A function is a module that you define in any appropriate place - you can keep it in the same button script, or on the card or stack to make it available to a wider range of handlers. A function will take data that a handler passes to it, do some processing and return a value to the handler that called it.
Let's take your code line by line and see where you need to get more understanding.
Code: Select all
put the currentChoice of button "Purchase Type" into tG4
-- here you are reading a custom property stored against the button "Purchase Type"
-- and putting it into a variable.
-- If you have never assigned the custom property value (called "currentChoice") then
-- your variable will be empty
put field "Price Input" into tG6
-- here you read the value in the field and put it into the variable tG6
if btn "Purchase type" = "Private" then
-- this does nothing useful , because btn "Purchase type" will never be equal to "Private"
-- as mentioned, one line number of the text of the button will be stored in the
-- menuHistory property of the button. Alternatively you could add a handler in the
-- button "Purchase type" script to store the whole line of text selected in the menu
-- choice, either by copying it to a field, or saving it as the custom property (see above)
-- you really need to be testing:
-- if tG4 = "Private"
-- but you first have to get something meaningful into tG4 (or whatever appropriate variable)
-- here your tests are fine, but there would be no point in having
-- put the value of field "Price Input" into variable tG6 if you are going to
-- type the whole lot out each time ;-)
if fld "Price Input" > 2000000.01 then
-- however, returning a value is no good here
-- where are you returning it?
return "7%"
-- what you are implying here (and on all the lines below) is
-- put "7%" into field "UO %age"
else if fld "Price Input" > 1000000.01 then
return "5%"
-- put "5%" into field "UO %age"
else if fld "Price Input" > 500000.01 then
return "4%"
-- put "4%" into field "UO %age"
else if fld "Price Input" > 250000.01 then
return "3%"
-- put "3%" into field "UO %age"
else
return "1%"
-- put "1%" into field "UO %age"
end if
else
return "15%"
-- put "15%" into field "UO %age"
end if
-- this does nothing as it tries to interpret two variables but comma separated and
-- not as a string literal
put (tG4, tG6) into field "UO %age"
-- the idea implied here is to call a function which would be a way to avoid having the repetitive put "x%" into field "UO %age" in all those lines above. Send off tG4 and tG6 to the function, have it determine what the outcome should be and return that value to us to put into the field.