1
我正在寻找一种方法来传递函数作为参数。将函数作为参数传递给VBA例如。根发现
说我有根查找算法的功能。该函数找到另一个函数f()的根。 现在我想能够传递任何函数作为参数,以便我可以使用具有不同函数的rootFinder函数(即,我已定义的n个函数中的任何函数)
如何传递f()它是各自的投入作为一个论点?
这里是我的代码来说明这个问题。我用f()和functionInputs作为占位符。
Function rootFinder(ByVal lowBound As Double, ByVal upBound As Double, ByVal f() As String, ByVal functionInputs As String)
'this function uses the
Dim xmid As Double, xold As Double, tol As Double
'accuary of algorithm
tol = 0.001
If f(lowBound,functionInputs) * f(upBound,functionInputs) > 0 Then
MsgBox "choose other bounds"
Else
xold = lowBound
Do
xmid = (lowBound + upBound)/2
If Abs((xmid - xold)/xmid) < tol Then
Exit Do
End If
xold = xmid
If f(lowBound,functionInputs) * f(xmid,functionInputs) > 0 Then
lowBound = xmid
Else
upBound = xmid
End If
Loop
rootFinder = xmid
End If
End Function
我使用了一个类似的函数传递一个数组(变体)。尝试像这样传递它',byval f as variant'。 (事件,如果它是一个字符串数组) – 2014-11-24 22:00:55