|
-
Feb 10th, 2024, 08:38 AM
#1
Thread Starter
Addicted Member
[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?
-
Feb 10th, 2024, 10:05 AM
#2
Re: Function as Parameter
You found it online where?
-
Feb 10th, 2024, 10:27 AM
#3
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.
-
Feb 10th, 2024, 10:33 AM
#4
Re: Function as Parameter
 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...
-
Feb 10th, 2024, 10:41 AM
#5
Thread Starter
Addicted Member
Re: Function as Parameter
 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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|