function returns

*Edit* how do i separate the code from the actual text?
Hi. i'm reading about classes and how to create fields, properties and methods so far i'm doing just far everything seems strait foward but one thing that i bumped into... a function must return a value in visual basic but in this example (http://msdn2.microsoft.com/en-us/library/f8f40dwh.aspx)

PrivateFunction CalcAge(ByVal yearAsInteger)AsInteger
CalcAge = My.Computer.Clock.LocalTime.Year - year
EndFunction

is CalcAge actually the equvalent of return CalcAge; ?

[906 byte] By [.Adrian] at [2007-12-25]
# 1

a function that returns a value - the signature of your method/function is correct, now you need to return the value back:

Dim CalcAge as Integer = My.Computer.Clock.LocalTime.Year - year

return CalcAge

this returns the value back to the caller - the return type must be the same as the function return type declaration

ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

There is nothing wrong with this function: the function will return the calculated value by assigning it to the function name (CalcAge):

Private Function CalcAge(ByVal year As Integer) As Integer
CalcAge = My.Computer.Clock.LocalTime.Year - year
End Function

This is functionally equivalent to:

Private Function CalcAge(ByVal year As Integer) As Integer
Return My.Computer.Clock.LocalTime.Year - year
End Function

You do not need to use 'return' to return a value (even though there are subtle differences).

SJWhiteley at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3
i wasn't asking what was wrong with the function just wanted to make sure that return CalcAge was the same as CalcAage = .....
also how do you separate the code from the text here?
.Adrian at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4
well I guess thats personal preference but I always, and many of my colleagues, always use the keyword return so we know its going to return something...I guess having a C# background enforces you to always use the return keyword when returning a value back to the caller (yes I know this is a VB.NET forum...)
ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5

to seperate code from text, enter:

[ code language = "vb" ]

[ /code ]

without the spaces, except between "code" and "language" tags

ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6
hehe yeah same. i never new that you could return something like that i always used return to return something. as i said thats not my code it's an example i'm following from msdn library and just wanted to make sure i understiid the concept :)
Thanks for all the replys, fast and helpful.
.Adrian at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7
This was the only way to return a value from a function with VB6 or earlier. That's why it remains. Additionally, it gives you greater versatility: you do not need to 'return' from the function immidiately after setting the return value (in essence, an intermediate variable is used to store the return value, and when the function ends/exits that 'variable' is returned).
SJWhiteley at 2007-9-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...