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: 
btpoole
Channel Surfer

Function Help

Having trouble with simple function. I can't seem to pass variable from one function to another. Is the example code correct in passing the value of d to the function main?
Additional info when i run the code below i get error saying use of uninitialized variable. I assume it is referring to d.
Thanks
function main () as object
getd()
a=2
b=3
c=(a+b) * d
print c
end function


Function getd () as object
d=1 + 2
return
end function
0 Kudos
2 REPLIES 2
RokuMarkn
Visitor

Re: Function Help

No, all variables are scoped to the function they are in. This means they're not visible from another function. To have getd change the value of d, you need to explicitly return it, like this:


function main () as object
d = getd()
a=2
b=3
c=(a+b) * d
print c
end function


Function getd () as integer
d=1 + 2
return d
end function


Also, if you want to pass a variable in the other direction, INTO a function, it needs to be declared as a parameter in the function definition, like this:


function AddOne(x as Integer) as Integer
return x+1
end function


--Mark
0 Kudos
btpoole
Channel Surfer

Re: Function Help

Thank you, that's just what I needed.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.