Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
crawfishmedia
Channel Surfer

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
0 Kudos
7 REPLIES 7
RokuChris
Roku Employee
Roku Employee

Re: Function, return two values

You could return an associative array with both values in it.
0 Kudos
EnTerr
Roku Guru

Re: Function, return two values

"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
0 Kudos
crawfishmedia
Channel Surfer

Re: Function, return two values

"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.
0 Kudos
EnTerr
Roku Guru

Re: Function, return two values

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
0 Kudos
destruk
Binge Watcher

Re: Function, return two values

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.
0 Kudos
TheEndless
Channel Surfer

Re: Function, return two values

"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().
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
crawfishmedia
Channel Surfer

Re: Function, return two values

I ended up using:
nextoff = nextoffset.ToStr()

nextoff is used in my url string
0 Kudos