Forum Discussion

crawfishmedia's avatar
crawfishmedia
Binge Watcher
12 years ago

Function, return two values

Hi guys, I am curious how to create a function that will return two values instead of just one.

I am passing in 'nextOffset', which is an integer value. I need to return the new 'nextOffset' value since its incremented, and I need to return 'nextoff', which is the string version of the nextOffset. The 'nextoff' string will be used in a URL later in the code. Any help is appreciated. Thanks.

Function SetNextOffset(nextOffset as Integer)
nextOffset = nextOffset+50
nextoff = Stri(nextOffset)
nextoff = nextoff.Trim()
return next off ' but I need to return nextOffset too...
End Function

7 Replies

  • You could return an associative array with both values in it.
  • "crawfishmedia" wrote:
    Function SetNextOffset(nextOffset as Integer)
    nextOffset = nextOffset+50
    nextoff = Stri(nextOffset)
    nextoff = nextoff.Trim()
    return next off ' but I need to return nextOffset too...
    End Function

    I can't imagine using such function to end well. It will be awkward to de-compose the returned result (be it hash or array). Better make an object that holds the offset as a state and add a method (say `getNextOffset`) that returns current offset as string and bumps up the offset
  • "EnTerr" wrote:
    "crawfishmedia" wrote:
    Function SetNextOffset(nextOffset as Integer)
    nextOffset = nextOffset+50
    nextoff = Stri(nextOffset)
    nextoff = nextoff.Trim()
    return next off ' but I need to return nextOffset too...
    End Function

    I can't imagine using such function to end well. It will be awkward to de-compose the returned result (be it hash or array). Better make an object that holds the offset as a state and add a method (say `getNextOffset`) that returns current offset as string and bumps up the offset


    Yes you're right. No matter what I still need minimum 3 lines of code in Main. Really all I wanted was an repeatable way to do the following since I need it many times:
    nextOffset = nextOffset+50
    nextoff = Stri(nextOffset)
    nextoff = nextoff.Trim()

    Unfortunately I can't turn this into a repeatable Sub or Function without adding complexity. I'll just keep duplicating these 3 lines.
    Closed.
  • expanding on what i suggested, can't you do something like

    offsetter = {
    nextOffset : 0,
    getNext : Function()
    m.nextOffset = m.nextOffset + 50
    return m.nextOffset.ToStr()
    End Function
    }

    x = offsetter.getNext()

    disclaimer: i have no real experience writing in brightscript. code snippet not tested and likely needs fixing.

    ps. and fixed per comments
  • destruk's avatar
    destruk
    Streaming Star
    offsetGen = {
    nextOffset : 0,
    next :

    "next" is a reserved word, so I don't think that would work.
    It might be easier to check MOD 50 to see if it is a valid offset value for the series, as well as being less than the total runtime length. Then you could bypass the need for the function entirely. It depends on what you need to use this for.
    If it is the number of lines you are concerned with, they can be combined from
    nextOffset = nextOffset+50
    nextoff = Stri(nextOffset)
    nextoff = nextoff.Trim()

    into

    nextOffset = nextOffset+50:nextoff = Stri(nextoffset).Trim()

    To use one line with two commands. I'm not sure which is faster to process.
  • "destruk" wrote:
    Stri(nextoffset).Trim()

    This could be shortened to "nextoffset.ToStr()" as the .ToStr() method doesn't add the leading space reserved for a sign, so no need for the Trim().
  • I ended up using:
    nextoff = nextoffset.ToStr()

    nextoff is used in my url string