Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
Have you studied encapsulation? That can also be a factor of whether a method has a return value or not. –  JB King Dec 28 '12 at 0:01
    
It depends upon whether you want to use return value in the calling function or not. –  Tilak Dec 28 '12 at 22:11
1  
A method is useful either because it produces a side effect or because it computes a value. Get in the habit of making every method you write do one or the other but not both if possible. Methods that produce side effects should be void; methods that compute values should return the value. –  Eric Lippert Dec 30 '12 at 7:30

3 Answers 3

up vote 7 down vote accepted

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.

share|improve this answer
9  
Agree with everything 100% except the final paragraph - naming conventions vary, and whilst I don't disagree with you, at least the OP was being consistent... –  Andrew Dec 28 '12 at 2:43
10  
This is C#. Instead of public int getSpeed() { return speed; } you should have public int Speed { get { return speed; } }. Also starting a method name in uppercase is standard convention in .NET. You seem to be treating this as if this was Java. –  luiscubal Dec 28 '12 at 2:46
1  
Methods are written in camel-case in C#, that is almost universally recognized coding style (which is also recommended by Microsoft). –  Matěj Zábský Dec 28 '12 at 9:18
1  
@MatějZábský No. The C# convention is PascalCase, not camelCase for methods. –  codesparkle Dec 30 '12 at 23:20
    
@codesparkle PascalCase = CamelCase en.wikipedia.org/wiki/CamelCase –  Matěj Zábský Dec 31 '12 at 0:13

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:

http://blogs.msdn.com/b/ericlippert/archive/2009/06/29/the-void-is-invariant.aspx

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.

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.