There’s something called Type System which is a set of rules that determine how the data will behave and what you can do with it. It is based on type theory.
So all language data structures are somehow based on these rules established by the language type system that you’re using.
So the rules for the void
are special. Does it make sense to be like this? I think it does. Would it make sense that he was a normal guy? He would do it. Every decision has its implication.
A return
can be used with nothing or can be used with an expression. Nothing can only be used if the method’s signature indicates that it should not return anything (with the void
). If the method is void
cannot return anything, and the syntax used is returning something.
Then you’ll say:
is not returning something, the method called is void
and void
indicates that there is nothing there
or say:
is returning void
for something that awaits void
Yes, but this is circumstantial, it can change. But the worst is that it suggests that something is being returned since hitting the eye on return algumacoisa
it seems that there is something there, only a deeper investigation will indicate that it is even appropriate.
Also it is a matter of consistency, in several places can not use so, because it would let use in the return
? What do I get?
So it was defined that whatever method void
can only be called where one expects statement and not where one expects an expression. This is more readable and works well without any negative points:
Test1();
return;
Want to put in a line?
Test1(); return;
I put in the Github for future reference.
Whichever Feature need to pay. There has to be a justification for your adoption, have to give a real benefit and not charge anything significant.
Informally we even say "the method returns void
", but this is conceptually wrong.
Other languages may adopt different posture.
Has proposal to allow this, but it was not very well accepted. Other.
Canonical answer.
I believe this answers the technical part Why can a void method in C++ Return a void value, but in other Languages it cannot?, on the passage awaiting certain "types".
– Guilherme Nascimento
Who negatived could make a contribution with a comment, talking about what can be improved in the question?
– Wallace Maxters