PDA

Click to See Complete Forum and Search --> : functions VS procedure


andrew104
Nov 14th, 2002, 10:47 AM
I have an assignment for my VB.NET class to compare functions and procedures and it has me completely confused. I wrote a program that does a bunch of math, etc. and I am required to change the following to a procedure, but all it does is mess up the whole program. Can someone please help me out a little here? Thanks,

Andrew

Private Function GetFwtTax(ByVal sngWeekPay As Single) As _ Single

-When I change Function to "Sub", the "As Single" shows erros.



What all do I need to change in the code? I am thoroughly confused about the difference between a function and procedure. HELP!!!!!!!! Thanks!

Edneeis
Nov 14th, 2002, 10:53 AM
A function returns a value (or CAN) where as a sub (procedure) doesn't (at least not directly). In a function the As blah blah blah is declaring the return type or the type of the value that it will return. Since a sub can't return a value like that it gives you an error. A sub can however change the value of an argument and expose those changes to the rest of the program if the argument is passed in ByRef.


'function way
Private Function GetFwtTax(ByVal sngWeekPay As Single) As Single
Return 1.1
End Function

dim ret as Single=GetFwtTax(1) 'returns 1.1

'sub way
Private Sub GetFwtTax(ByVal sngWeekPay As Single,ByRef sngReturn as Single)
sngReturn=1.1
End Sub

dim ret as Single
GetFwtTax(1,ret) 'ret now equals 1.1