pepe90
09-23-2011, 09:28 AM
Hello, is there a way to create a function that yields multiple outputs? I'm thinking on one that yields an array of values which correspond to each output but I doesn't know if it'll work. Is there another way to do this?
Thanks in advance
mikerickson
09-23-2011, 12:52 PM
Sure, consider this function.
Function RectToPolar (ByVal x As Double, ByVal y As Double, ByRef R as Double, ByRef theta as Double) As Boolean
theta = Atn(y/x)
R = Sqr(x^2 + y^2)
RectToPolar = True
End FunctionIt would be used like:
Sub test()
Dim radius As Double, angle As Double
Dim xCoord As Double, yCoord As Double
xCoord = 6: yCoord = 3
RectToPolar xCoord, yCoord, radius, angle
MsgBox xCoord & ", " & yCoord & " is at angle " & angle & ", radius " & radius
End Sub
compare that to this formulation, that returns an array
Function RectToPolar2(x As Double, y As Double) As Variant
RectToPolar = Array(Atn(y / x), Sqr(x ^ 2 + y ^ 2))
End Function
Sub test2()
Dim xCoord As Double, yCoord As Double
Dim PolarCoords As Variant
xCoord = 6: yCoord = 3
PolarCoords = RectToPolar(xCoord, yCoord)
MsgBox xCoord & ", " & yCoord & " is at angle " & PolarCoords(0) & ", radius " & PolarCoords(1)
End Sub
(Yes, I know that both those functions will error if y=0, and will give wrong results if x<0 and y<0 and....)
pepe90
09-23-2011, 01:20 PM
Thank you. I havenīt tried it because it isnīt an urgent issue but it seems to have all the sense.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.