I want to uderstand why there is a method in C# that could reurn a value, for example:
public int Accelerate()
{
Speed++;
return Speed;
}
and a method that does not reurn a value (void)?
What is the difference in the following example of the above one:
public void Accelerate()
{
Speed++;
Console.WriteLine(Speed);
}
I see that last one will save us a time rather than defining a variable to hold this field in when creating a new object! I’m beginner, so could anyone explain?
4
The second one only works within a console based app. It assumes you want to print the speed in a console screen.
The first one will work on a console, or as part os a web app, or as part of a GUI based desktop app, or mobile app.
The first one is therefore more useful as part of a modular system.
The second one is doing a calculation and also printing to screen, mixing the business logic with the presentation, which is bad.
That said, first method shouldn’t even return the value. A separate getSpeed()
method should exist to return that value. Accelerate
should only increment the speed.
Also the variable should be named speed
in lowercase and the method whould be called accelerate()
also in lowercase.
6
From a real basic programming perspective, in C# if you need a method to get, derive, produce, instantiate, or otherwise communicate something back to the caller you use a function with the appropriate return type.
If you need a method to just do something and you don’t need anything from it, use a void.
Too Long; Did Not Read Info:
Functions return control to the caller upon completion along with a value or reference to some kind of product; a void is a special case function that has no corresponding product…it is kind of the type equivalent of null.
Some languages specifically differentiate between functions (methods that return a value) and subroutines (methods that do not return a value). C based languages instead use “void” to differentiate between them.
In C# specifically, void is internally mapped to a struct with some strange compiler enforced behavior; you can’t really use this type for anything useful but if you are doing reflection you might notice it and think “hrm?”:
http://msdn.microsoft.com/en-us/library/system.void.aspx
Eric Lippert, until recently a principal developer on the C# compiler team, had some interesting things to say about void a few years ago; it might provide some insight for you:
Link
Note that you could simulate functions using voids, via out or ref parameter(s) but this would be needlessly verbose and I daresay inelegant; functions are much cleaner and easier to read and write.
Personally, when I use a void it is usually for a private or protected method or to follow a design pattern. I tend to favor functions for public methods; even if only to return true / false to allow a feedback loop for the caller(s) or to assist in unit testing.
because in most applications you want to do something with the new speed, like finding the relative speed between 2 objects, the your code there is no way to get the new speed (unless you have a separate method for that
also if you wrote everything out to the console you’d have a ton of spam on it, take minecraft it runs at 20 “ticks” a second with up to a hundred different moving entities, having 2000 messages of speed or position per second in the log is utterly useless and writing this out will lag the game
for another point it is not the responsibility of the accelerate method to write things out to the console