# Putting a function in the parameter list of a subroutine?

• ### Question

• In a plotting subroutine, I need to pass a function of the thing to plot.For example, a skeleton program would look like this:-

sub plot(func_to_plot)

for n=0 to 10

value= func_to_plot(n)  'to plot a squaring function

' carry out plot of value

next n

end sub

func_to_plot(v as integer) as integer

return v^2

end function

But I can't get this syntax to work.

Gord

• Edited by Monday, April 9, 2012 1:08 AM minor change, added "value"
Monday, April 9, 2012 12:18 AM

• Also the line  plot(Function(v) v * v) is difficult to understand because it duplicates the func_to_plot routine.

Yes, that's what I wrote as a comment below the code. It's another way to do the same.

Unfortunatelly I don't know why you see the listing truncated. I could make a screenshot but maybe that screenshot gets truncated, too. ;-)

Armin

• Marked as answer by Monday, April 9, 2012 6:31 PM
Monday, April 9, 2012 12:55 PM

### All replies

• Hi GordC,

I'm not exactly sure on what it is you are trying to do but the following code calls your function and returns the values to a listbox.

Copy the code in a new project, add a button and a ListBox then run and click on the button. You should be able to work out what you require from this.

Regards Ron

```Public Class Form1
Sub plot() 'ByVal func_to_plot)
Dim value As Integer
For n = 0 To 10
value = func_to_plot(n)  'to plot a squaring function
' carry out plot of value
Next n
End Sub
Function func_to_plot(ByVal v As Integer) As Integer
Return v ^ 2
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
plot()
End Sub
End Class```

• Edited by Monday, April 9, 2012 4:39 AM
• Proposed as answer by Monday, April 9, 2012 9:04 AM
Monday, April 9, 2012 4:34 AM
• If it's not Ron's approach, maybe you're looking for a Delegate:

```Public Class Main

Shared Sub Main()

plot(Function(v) v * v)

End Sub
Shared Sub plot(ByVal func As Func(Of Integer, Integer))

Dim value As Integer

For n = 0 To 10

value = func(n)  'to plot a squaring function

' carry out plot of value

Next n

End Sub
Shared Function func_to_plot(ByVal v As Integer) As Integer

Return v * v

End Function
End Class```

Note that in Sub Main, the first call passes a new delegate to the function func_to_plot. The second call implicitly declares a function returning the same value, and passes a delegate to that function.

Armin

Monday, April 9, 2012 11:56 AM
• OK Ron, Your approach is what I have been using and it works well as one would expect. But it is not the best approach when one has to plot 2 or more different functions because the generating func_to_plot is embedded in the plotting subroutine.

I am trying to understand Armin's solution because it seems that's what I need. The listing there is truncated on the line:-

Shared Sub plot(ByVal func As Func(Of Integer, Integer,I

Also the line  plot(Function(v) v * v) is difficult to understand because it duplicates the func_to_plot routine. But I will try it and get back to you

GordC

Monday, April 9, 2012 12:37 PM
• Also the line  plot(Function(v) v * v) is difficult to understand because it duplicates the func_to_plot routine.

Yes, that's what I wrote as a comment below the code. It's another way to do the same.

Unfortunatelly I don't know why you see the listing truncated. I could make a screenshot but maybe that screenshot gets truncated, too. ;-)

Armin

• Marked as answer by Monday, April 9, 2012 6:31 PM
Monday, April 9, 2012 12:55 PM