As much as possible, functions should not display anything, they should simply return a value which you can then use inside one of the objects in your template.
Assuming your current code produces the proper numeric value, you could modify it so it looks like this:
function @OFFER_PRIC(): string
Define(&total, currency, 0)
Define(&i, integer, 0)
for(&i, 1, 1, subreccount())
&total:= &total + strtocur(Field('OFFER_PRIC', &i))
endfor()
&result:=@FormatAmountCurr(&total,',')
endfunction()
Then in the calling object, simply use:
But being a programmer at heart, I don't much like this because the OFFER_PRIC function then performs two separate tasks: totaling fields AND formatting the result.
My preferred way of doing this would be:
function @OFFER_PRIC(): currency
Define(&total, currency, 0)
Define(&i, integer, 0)
for(&i, 1, 1, subreccount())
&total:= &total + strtocur(Field('OFFER_PRIC', &i))
endfor()
&result:=&total
endfunction()
Now, the funtion does only one thing: it returns the total value as a currency.
You can then use that value anywhere you need the total, and when you want to display it in a formatted manner, you would use:
show(@FormatAmountCurr(@OFFER_PRIC(),','))
This way, each function performs a single task and nesting them as shown above allows you to use different combinations.
Note: make sure that you have imported both the
FormatAmountCurr and
FormatAmountStr global functions as the former depends on the latter.