[RESOLVED] Function as Parameter
Hello, I'm trying to use this method that I found online:
Code:
Function Bisect(a as Double, b As Double, f As Function(Of Double, Double)) As Double
Dim c As Double = (a + b) / 2
Dim d As Double = f(c)
While Math.Abs(d) >= 0.0001
If Math.Sign(d) = Math.Sign(f(a)) Then
a = c
Else
b = c
End If
c = (a + b) / 2
d = f(c)
End While
Return c
End Function
But VS say Keyword does not name a type.
I thing the signature is wrong.
Any advice?
Re: Function as Parameter
You found it online where?
Re: Function as Parameter
Either the person who posted it made a mistake typing it or you did. That should almost certainly be this:
Code:
Function Bisect(a as Double, b As Double, f As Func(Of Double, Double)) As Double
An Action is a delegate for a Sub, i.e. a method that doesn't return a value, and a Func is a delegate for a Function, i.e. a method that does return a value. Both can have zero to 16 parameters. I suggest that you read the documentation for each.
Re: Function as Parameter
Quote:
Originally Posted by
hannibal smith
Hello, I'm trying to use this method that I found online:
Code:
Function Bisect(a as Double, b As Double, f As Function(Of Double, Double)) As Double
Dim c As Double = (a + b) / 2
Dim d As Double = f(c)
While Math.Abs(d) >= 0.0001
If Math.Sign(d) = Math.Sign(f(a)) Then
a = c
Else
b = c
End If
c = (a + b) / 2
d = f(c)
End While
Return c
End Function
But VS say Keyword does not name a type.
I thing the signature is wrong.
Any advice?
I suspect the declaration should be
Code:
Function Bisect(a As Double, b As Double, f As Func(Of Double, Double)) As Double
Edit: jmcilhinney beat me to it, my own fault for having too many tabs open...
Re: Function as Parameter
Quote:
Originally Posted by
jmcilhinney
Either the person who posted it made a mistake typing it or you did. That should almost certainly be this:
Code:
Function Bisect(a as Double, b As Double, f As Func(Of Double, Double)) As Double
An Action is a delegate for a Sub, i.e. a method that doesn't return a value, and a Func is a delegate for a Function, i.e. a method that does return a value. Both can have zero to 16 parameters. I suggest that you read the documentation for each.
Thanks. Now it work.