#Why is "return" not working in this function?

1 messages · Page 1 of 1 (latest)

junior shell
#

This function is producing the error message "The return effect can only be used in functions, custom expressions..." and I don't know what the issue is.

function toInt(rawNumber: text):
    set {_value} to {_rawNumber}
    replace "," in {_value} with ""
    if {_rawNumber} contains "k" or "m" or "b":
        replace "k" in {_value} with "000"
        replace "m" in {_value} with "000000"
        replace "b" in {_value} with "000000000"
        if {_rawNumber} contains ".":
            set {_numbers::*} to {_rawNumber} split at "."
            replace "k" or "m" or "b" or "." in {_numbers::2} with ""
            set {_numLength} to length of "%{_numbers::2}%"
            replace "." in {_value} with ""
            set {_numValue} to {_value} parsed as integer
            set {_numValue} to {_numValue} / 10 ^ {_numLength}
            set {_value} to "%{_numValue}%"
    set {_numValue} to {_value} parsed as integer
    return {_numValue}
lyric pewter
#

its because your function is not defined as having a return type

junior shell
#

So it needs to be
function toInt(rawNumber: text) :: integer:

#

Alright, it works now. Thank you!