void why is it needed as a return type?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().
 
Hi,

It is the C# lenguage definition, and also making things explicit improves
the code understanding.

Best regards
Salva
 
How would you differentiate the following

class Foo
{
// this is a default constructor
public Foo()
{
}
// this is a method called Foo that returns nothing
public Foo()
{
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Just curious, we no longer require void on a method that takes no
parameters;

void methodWithNoParameters(void) has been replaced by void
methodWithNoParameters().

Is there a good reason we still have to specify void for a method that
returns no result? For example;

private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().
 
Salvador,
Just because it's the language definition doesn't mean it's the best
way of doing things. Also you are explicitly not having a return type
on the method prototype and so you're explicitly saying there's no
return type.

Richard,
Good point. In way of doing things you'd not allow methods named the
same as the class they are being called on - it'll throw a compiler
error. Can you thing of a good reason why a method would have to be
called the same as the class?

Either way it's no big deal...
 
Hi,

I haven't say that the lenguage definition is the best way to do things,
it is just a definition, you are free to choose your lenguage, there is no
performance implication on this, actually if you generate your own lenguage
based on ASM your return type is a memory address. What C# is doing is
reseting the address with 0000000 on the heap, specially assigned for void
type, when you assign void this address is protected so you can not use it.
At the end, no performance implication, is only a matter of how the lenguage
understand the things. You can also ask why the lenguage use the "for"
statement instead of (int i = 0; i< x; i++) without a for? It is just a
matter of definitions , that's all.

Regards
Salvador
 
I guess the issue is that to some degree not allowing a method the same name as the type of a fairly arbitrary restriction and when combined with C# tending to favor explicit syntax rather than implied behavior would be counter-intuitive to the rest of the language.

Another issue is if I have

class Bar
{
public Foo()
{
}
public Bar()
{
}
}

if I then rename the type as Foo then suddenly Foo becomes a constructor and i may miss that and get very strange behavior when I create instances

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


Richard,
Good point. In way of doing things you'd not allow methods named the
same as the class they are being called on - it'll throw a compiler
error. Can you thing of a good reason why a method would have to be
called the same as the class?
 
Well, I have another opinion. I program in Delphi as well and in that
language you have 2 different types of functions. You have what are called
functions which would return something and you have things called procedures
which do not. However, to declare these things you have to type out
function or procedure in front of the function name so the compiler knows
what you plan to do.

So taking that into account I think that is the answer. The reason that its
necessary to specify a return type is so that the compiler can be sure of
your intentions. Parameters are not that important because you either have
them or you don't, but trying to return something when you said you were not
going to, or perhaps not returning something when you said you were could
cause problems in other places. By forcing you to tell the compiler up
front what sort of contract you will have with a particular function you
allow it to always know that you do what you said you were going to do.

Otherwise it would take it longer to do its job because it would have to
determine your intentions only after analysing the entire code section...
Also when looking at calling things from external modules it could just be a
runtime error because there may be cases where that determination can't be
made at design time due to functions being contained in COM objects or other
various places...

just my 2 cents,

glenn
 
To think... all of this discussion because of 4 simple characters (v-o-i-d)?

I'm at a loss for words... lol

~d
 
private void methodWithNoReturn() could been replaced by private
methodWithNoReturn().

Well, first of all, "private" is already assumed so
private methodWithNoReturn() {...}
could be written as:
methodWithNoReturn() {....}

But, in C/C++ that means "returning a integer". When you have identical
syntax, you should have them to the same thing.

--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
Typing four characters is not the issue. It's the "why" that I'm
interested in? It think Richard's comment about refactoring and
constructors probably is the best reason for explicit return types is
I've heard so far.
 
Back
Top