• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Functions in VB

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

knight_of_knee

Member
Joined
Mar 20, 2005
Location
Springfield, Missouri
Ok I might just be retarded but for some reason whenever I try to do a function in VB it never returns the value that I tell it to. So I'm probably missing something very obvious but I don't know what it is so any and all help would be nice here's the code. BTW I'm using VB.net 2005:

Sub Research()
Dim sr As IO.StreamReader = IO.File.OpenText("Research.txt")
Dim wage, hour, gross, ded, net, totgross, totded, totnet As Double
Dim name As String
Do While sr.Peek <> -1
name = CStr(sr.ReadLine)
hour = CDbl(sr.ReadLine)
wage = CDbl(sr.ReadLine)
Call Grosspay(hour, wage, gross)
Call Deduction(gross, ded)
Call Netpay(gross, ded, net)
totalnetpay(totnet, net
Function totalnetpay(ByVal totalnet As Double, ByVal net As Double) As Double
totalnet = totalnet + net
Return totalnet
End Function

Grosspay, Deduction and Netpay are all sub procedures that i used ByRef on instead and it returns all the values that it is supposed to correctly except for the totalnetpay. Thank you to anyone that helps.
 
Last edited:
There's something odd in your example after Call NetPay since there is no closing parenthesis and no End Sub for sub Research. It just kind of ends. But that aside, it looks like you have no place to put the return value of a function. Something like:

Dim RV as Double
RV = TotalNetPay(TotNet, Net)

You could simplify that these days I think by just doing:
Dim RV as Double = TotalNetPay(TotNet, Net)

In either case, RV now has the value returned by the function.

A function called with no assignment operator (the = sign) acts like a sub and returns no value. You need to give it somewhere to put the return value. In C# (and many other languages) everything is a function, but if it is declared as Void then it returns no value. VB just calls it a sub instead.

One more thing. I don't think it makes any difference in this example, but it is generally bad form to muck with the parameters that have been passed in, even if done ByVal. With a Value type variable like Double, you're safe. But with a Reference type variable, like an object of some sort, ByVal does not protect you. If you change a property of an object that was passed ByVal, you are still changing the original object. You are not getting a copy of the object. All ByVal does in that case is protect the pointer to the object. So if totalnet was a widget instead of a double, and you changed its Color property (for instance - assuming it has a Color property), outside of that function the Color was changed.

If, however, inside the function you did this:

totalnet = new widget()

totalnet would now point to a new object inside the function, but outside the function the calling routine would still be accessing the original widget.

Clear as mud?
 
Yup as clear as mud haha. But I think I understand what you're trying to get at. I'm just learning vb for one of my classes in college I'm a freshman and this is the first language that I actually know how to do something on, even though not a whole lot. So I am just trying to get the feel of it, but thank you for the help.
 
What part was disgusting? The part about byval with objects is the same as all DotNet languages, and I think is the same in non-dotnet languages. Cloning an object can be a very expensive proposition and isn't something you want to do if you can avoid it.

I've learned lots of languages over time and they're all pretty cool in some way. I've heard folks argue that VB is too verbose, but I sometimes work in a language called MUMPS where this line:

i i i s s i=s d x q:j d @x

is a perfectly valid piece of code. Everything in life, and code, is a balance.

So what part of the code did you find disgusting?
 
Back