Function return question.

T

Thomas Scheiderich

I am curious as to why ASP.NET returns values a different way from VB or
VB.net (or can you use both).

In my one book I have it returning using a return statement

***********************************************************
function RollDie As Integer
Dim Roll As Integer
Randomize
Roll = Int(rnd * 6) + 1
Return(roll)
end function
***********************************************************

instead of return it only with the assigment to the function name:

***********************************************************
function RollDie As Integer
Dim Roll As Integer
Randomize
Roll = Int(rnd * 6) + 1
end function
************************************************************

Are both of these correct?

Thanks,

Tom.
 
C

Chad Z. Hower aka Kudzu

Thomas Scheiderich said:
I am curious as to why ASP.NET returns values a different way from VB or
VB.net (or can you use both).

In my one book I have it returning using a return statement

***********************************************************
function RollDie As Integer
Dim Roll As Integer
Randomize
Roll = Int(rnd * 6) + 1
Return(roll)
end function
***********************************************************

instead of return it only with the assigment to the function name:

***********************************************************
function RollDie As Integer
Dim Roll As Integer
Randomize
Roll = Int(rnd * 6) + 1
end function
************************************************************

Are both of these correct?

Assuming the second one is RollDie instead of Roll, they are just alternate
syntaxes AFAIK.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
S

Scott M.

The VB 6.0 way was to assign the function name to a value. In .NET, you
use: "Return someValue".

So,

Return Roll

....and...

RollDie = Roll

would return the same value (in your second example you didn't set the
function equal to anything, so you would get any value from your second
version).

The difference between the two is that "Return" not only set the value of
the function, but it also exits from the function after it executes, so any
code written after Return will not run, whereas by setting the return value
= to something where all the code will run up to the End Function statement.

For example:

Function RollDie As Integer
Dim Roll As Integer
Dim y as new Random
Roll = y.next(1,6)
Return Roll
'This next line will never execute because the function will "Return"
control back to the calling procedure after the above line.
Dim x as String = "Test"
End Function

Also note the use of the new .NET Random class and its next method as
opposed to the VB 6 Randomize() and Rnd() functions.
 
T

Thomas Scheiderich

Scott said:
The VB 6.0 way was to assign the function name to a value. In .NET, you
use: "Return someValue".

So,

Return Roll

...and...

RollDie = Roll

would return the same value (in your second example you didn't set the
function equal to anything, so you would get any value from your second
version).

The difference between the two is that "Return" not only set the value of
the function, but it also exits from the function after it executes, so any
code written after Return will not run, whereas by setting the return value
= to something where all the code will run up to the End Function statement.

For example:

Function RollDie As Integer
Dim Roll As Integer
Dim y as new Random
Roll = y.next(1,6)
Return Roll
'This next line will never execute because the function will "Return"
control back to the calling procedure after the above line.
Dim x as String = "Test"
End Function


My mistake here as the line should be:

RollDie = Int(rnd * 6) + 1


Which does work.

Also note the use of the new .NET Random class and its next method as
opposed to the VB 6 Randomize() and Rnd() functions.

I'll definately need to look into these.

Thanks,

Tom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,099
Messages
2,570,626
Members
47,237
Latest member
David123

Latest Threads

Top