Tuesday, January 06, 2009     | Register
TradersStudio Forums
Subject: Multiple outputs from a function

You are not authorized to post a reply.   
Author Messages
thimel1
Posts:14

08-27-2007 4:27 AM Alert 

Is it possible to have multiple outputs from a function?  In TradeStation, this is done by having function arguments used for outputs. If it is possible, what is the syntax, both for the function declaration and the calling routine?

Thanks.

murray
Posts:435

08-29-2007 11:19 AM Alert 

Yes, you need to declare the variables by ref.

In general functions look for this one as your example

Function ParabolicSAR(AfStep, AfLimit, ByRef oParCl, ByRef oParOp, ByRef oPosition, ByRef oTransition) As BarArray

 

 

rdencpa
Posts:10

08-29-2007 1:54 PM Alert 
If I understand correctly, the ByRef means the function can change the value that was passed to it, but I don't follow how you get two or more outputs from the function.

Could you post a simple expample of calling a 2 output function together with the function code?

Thanks
murray
Posts:435

08-31-2007 11:46 PM Alert 

Please look at the sample code for the function I talked about able. It's in TradersStudio. If you look at the calling system and the function code it will be clear, it returns multiple values because you can set them in the function and use them in the calling program.

 

fobos
Posts:4

09-01-2007 9:13 AM Alert 
There are two ways to pass parameters to function: by values and by reference. In former case a function would work with the copies of the original parameters, thus no changing the originals. In the latter case a function would work directly with the originals, modifying them, and this can be used for returning multiple values from the function.
For example:
You have somewhere else two functions f1 and f2 defined

function f1(byval v1, byval v2r)
dim a as integer
a = v1
v1=v2
v2=a
' return anything what you like here
f1=v1+v2
end function

function f1(byref r1, byref r2)
dim a as integer
a = r1
r1=r2
r2=a
' return anything what you like here
f2=r1+r2
end function

dim e1 as integer = 2
dim e2 as integer = 4
dim ret as integer = 0

ret = f1(e1, e2) 'pass by value
' The result here: ret = 6, e1 = 2, e2 = 4. As you see though the f1 swapped the values inside, the outside values remain the same

ret = f2(e1, e2) 'pass by reference
' The result here: ret = 6, e1 = 4, e2 = 2. As you see the f2 actually swapped the original variables outside of the function
' So here you returned as many as three results: ret, e1, e2. Use e1 and e2 as your additional returns from the function f2.
If you want to pass two entries and have two new results then you would declare a function like this
function myFunction(byval p1, byval p2, byref p3, byref p4), where p1 and p2 are your entries, and p3 and p4 are your results
You are not authorized to post a reply.
Forums > TradersStudio 2.x > TradersStudio Basic > Multiple outputs from a function



ActiveForums 3.6
TradersStudio® Copyright 2004-2009 All Rights Reserved   |  Privacy Statement  |  Terms Of Use