What's the difference between a function and a sub?

G

Guest

I am a newbie to C# and have come across some VB .NET code in my studies. I
notice that VB uses functions and subs, and was wondering what the difference
was. Is it that one has a return value and the other doesn't?
 
A

Armin Zingler

cashdeskmac said:
I am a newbie to C# and have come across some VB .NET code in my
studies. I
notice that VB uses functions and subs, and was wondering what the
difference was. Is it that one has a return value and the other
doesn't?

Yes, it is.

Armin
 
M

m.posseth

well with byref parameters a sub can also return values ,,,,

maybe a better explanation if you can follow me , is that a sub is a
method that doesn`t return values by its method name , and a function is a
method that can return values by it`s method name
 
A

Armin Zingler

m.posseth said:
well with byref parameters a sub can also return values ,,,,

Yes, but not as the function value. That's what he meant (probably).
 
H

Herfried K. Wagner [MVP]

m.posseth said:
well with byref parameters a sub can also return values ,,,,

Even a 'ByVal' parameter of a reference type can do that ;-).
 
C

Cor Ligthert

Herfried,
Even a 'ByVal' parameter of a reference type can do that ;-).
No it does not "return" a value, those can be set.

A sub is just a void function.

Cor
 
C

Cor Ligthert

Michel.

I don't agree with you, a void function (in VBNet a Sub) does not "return" a
value. You can set values, however those are not returned.

You can use them afterwards, however that I see as a complete different
approach, as there are than more possibilities. (By instance set values in
modules or whatever)

Just my thought,

Cor
 
C

Cor Ligthert

It depends on how you define "returing a value".

Maybe in Thuringen however for the rest of the world it is well knowed.

A = B where B returns a value to A.

Cor
 
H

Herfried K. Wagner [MVP]

Cor,

Cor Ligthert said:
Maybe in Thuringen however for the rest of the world it is well knowed.

A = B where B returns a value to A.

I still believe that using output parameters is one technique to "return"
values.
 
A

Armin Zingler

Herfried K. Wagner said:
Cor,



I still believe that using output parameters is one technique to
"return" values.


Yes, but talking about the difference between subs and functions, the
statement "Is it that one has a return value and the other doesn't?" it's
for sure related to the function return value. I think you know this. :)

Armin
 
H

Herfried K. Wagner [MVP]

Armin Zingler said:
Yes, but talking about the difference between subs and functions, the
statement "Is it that one has a return value and the other doesn't?" it's
for sure related to the function return value. I think you know this. :)

Yeah, I know this :).
 
M

Michael D. Ober


This is also referred to as "side-effect" programming. There are times when
it's necessary, but as a general rule, it is harder to maintain code that
returns values in parameters. The most common usage for this is in a
function that returns a status code and puts a value (usually a string or
byte array) in a passed parameter. The windows API is full of these.

Mike Ober.
 
C

Carlos J. Quintero [.NET MVP]

Michael D. Ober said:
This is also referred to as "side-effect" programming. There are times
when
it's necessary, but as a general rule, it is harder to maintain code that
returns values in parameters.

Java? ;-)

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Joined
Sep 4, 2009
Messages
1
Reaction score
0
it is NOT a way to return a value... since you dont actually "return" a value but u change the value of a object(since ur using a reference) ... as already mentioned this is "side effect programming" and if u dont seriously need to, DONT DO IT !!!!

p.s.: i just noticed that this thread hasn't been answered to in years, it just made me, well, angry... :mad:
 
Last edited:

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

Top