I dunno if it's possible, but I'd like to pass a function name as an argument to a function.
The idea is to use the result of the function to do some calculations, no matter what the function is. It would be like this:
Function Find_Zeros( Function_Name As Function, Value_X As Double, etc)
Thanks in advance.

Passing Function As Argument
Adrian Foot
I'm okay with passing strings, as long as it works
This function is to find the root of the function, if there's only one root, and if the values of the function in left and right are of different signals. It's a rude way to find roots, but it's the only method that I know that always find a solution (tangent and newton-raphson sometimes don't find any).
It explodes, yes, but the validation is made outside the function =]
Thx Andy, once again
arcoant
I did not know what the values of Left and Right should be. With both at zero the function runs out of stack.
Sub Test()
MsgBox Find_Zero("Function1", Left, Right)
MsgBox Find_Zero("Function2", Left, Right)
End Sub
Function Function1(Value) As Double
Function1 = Value - 10
End Function
Function Function2(Value) As Double
Function2 = Value
End Function
Public Function Find_Zero(Function_X As String, Left As Double, Right As Double, Optional Precision As Double = 1 / 1000)
Dim Value_at_Mid As Double
Dim Mid As Double
Mid = (Left + Right) / 2
Value_at_Mid = Application.Run(Function_X, Mid)
If Value_at_Mid <= Precision And Value_at_Mid >= -Precision Then
Find_Zero = Value_at_Mid
Else
If Value_at_Mid * Application.Run(Function_X, Left) > 0 Then
Left = Mid
Else
Right = Mid
End If
Find_Zero = Find_Zero(Function_X, Left, Right)
End If
End Function
carlop
Nono =]
What I want to do is like this:
Sub Test()
MsgBox FindZero(Function1, Left, Right)
MsgBox FindZero(Function2, Left, Right)
End Sub
Function Function1(Value) As Double
Function1 = Value -10
End Function
Function Function2 (Value) As Double
Function2 = Value
End Function
Public Function Find_Zero(Function_X As Function, Left As Double, Right As Double, Optional Precision As Double = 1 / 1000)
Dim Value_at_Mid As Double
Dim Mid As Double
Mid = (Left + Right) / 2
Value_at_Mid = Function_X (Mid)
If Value_at_Mid <= Precision And Value_at_Mid >= -Precision Then
Find_Zero = Valor_do_Meio
Else
If Value_at_Mid * Function_X(Left) > 0 Then
Left = Mid
Else
Right = Mid
End If
Find_Zero = Find_Zero(Function_X,Left, Right)
End If
End Function
The MsgBoxes would show almost 10, and almost zero. (almost bc of the precision =])
Test1234525
Is this the sort of thing you mean The result of the 1st function is passed into the 2nd function.
Sub Test()
MsgBox myFuncPower(myFunc10(3), 2)
MsgBox myFuncPower(myFunc3(3), 1 / 2)
End Sub
Function myFunc10(Value) As Variant
myFunc10 = Value * 10
End Function
Function myFunc3(Value) As Variant
myFunc3 = Value * 3
End Function
Function myFuncPower(Value, Npower) As Double
myFuncPower = Value ^ Npower
End Function