4

So this question follows on from a previous post that I am just trying to understand in full before I move on to more complicated C# stuff.

My question relates specifically to the return value of a function.

Consider the following function code:

public static void DisplayResult(int PlayerTotal, int DealerTotal){
        if (PlayerTotal > DealerTotal) {
            Console.WriteLine ("You Win!");
            Console.ReadLine ();
        } 

        else if (DealerTotal > PlayerTotal) {
            Console.WriteLine ("Dealer Wins!");
            Console.ReadLine ();
        } else {
            Console.WriteLine ("It is a Draw!");
            Console.ReadLine ();
        }

I could be wrong of course but I believe that the "void" keyword in the first line of code means that the function code result does NOT return a value.

What I am trying to understand is - the function calculates a result. It distributes text (eg: "you win!" etc) based on the result. Is the result of the function not considered a value?

By my own (novice) logic, I would have thought one of two things:

  1. The return value of this function is a string because it is the output of the calculated result.
  2. The return value of this function is an int because it calculates int results.

I hope this makes sense. I think understanding this concept will make it easier for me in future to write functions without second guessing return values. If anyone has an example of a function that actually DOES return a value it would also be appreciated.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Nicole Haines
  • 191
  • 1
  • 9
  • 1
    in my opinion it should not return anything as the function name says `DisplayResult` – Sudhakar Tillapudi Mar 21 '14 at 05:02
  • Yep, as Sudhakar said, you might want to have `int CheckWinner(int playerTotal, int dealerTotal)` function that returns `1` for win, `0` for draw, and `-1` for lose. Then you have a function `void DisplayResult(int status)` to print those result. – Rosdi Kasim Mar 21 '14 at 05:05
  • Some seriously fantastic explanations on here everyone - thank you. I understand what a return value is actually supposed to DO now - return a value to the method that can be called on later. It also allows for return values to be defined in variables for any other methods to call on. Thanks all! – Nicole Haines Mar 21 '14 at 23:06

3 Answers3

5

A bit of terminology first: C# doesn't have functions, it has methods.

Return values give some value to the method's caller. They aren't related to the statements executed within that method, except for the return statement. The method's signature dictates what (if any) type is returned by that method.

public void NoReturnValue()
{
    // It doesn't matter what happens in here; the caller won't get a return value!
}

public int IntReturnValue()
{
    // Tons of code here, or none; it doesn't matter
    return 0;
}

...

NoReturnValue(); // Can't use the return value because there is none!
int i = IntReturnValue(); // The method says it returns int, so the compiler likes this
NathanAldenSr
  • 7,841
  • 4
  • 40
  • 51
  • Method - got it. I won't make that mistake again, thank you for the clarification. And this is a great example of methods with and without a return value. Thank you. – Nicole Haines Mar 21 '14 at 23:07
  • C# does have functions because it has functional programming support. You can read more here on SO: [What's the difference between method and function](http://stackoverflow.com/questions/155609/what-is-the-difference-between-a-method-and-a-function) – Measurity Mar 22 '14 at 00:14
  • In C#, all methods must be defined within classes; that's why they're called methods. You can have delegates and lambda expressions, but those aren't functions in the C sense of the word. – NathanAldenSr Mar 22 '14 at 01:31
4

A void method, as you have surmised, is one that returns no value. If you wrote:

var myResult = DisplayResult(3, 7)

you would get an error because there is no returned value to assign to myResult.

Your method outputs text to the console. This is a "side effect" of the method, but has nothing to do with its return value.

Also, the fact that your method interacts with ints has nothing to do with what its return value is either.

As a final point. The most basic thing to keep in mind is that a return value is a value that comes after the return keyword:

return "All done!";

Anything that doesn't involve the return keyword is not a return value.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • Thank you. This has really helped me to understand the use of return values in variables and how it is supposed to work. Hopefully with a bit of thime this will all get a lot clearer! – Nicole Haines Mar 21 '14 at 23:08
0

calling Console.writeline is not returning from the DisplayResult method/function

what you have is 3 execution paths only one of which can happen. after it happens the method/function returns void not a string

Tip:

you could get rid of the Console.WriteLine and .ReadLine and replace it with return "result of if statment"; then call your method/function like Console.WriteLine(DisplayResult(/*params*/)); which means you only write Console.WriteLine()/.ReadLine() once

public static string DisplayResult(int PlayerTotal, int DealerTotal){
    if (PlayerTotal > DealerTotal) {
        return "You Win!"
    } 

    else if (DealerTotal > PlayerTotal) {
        return "Dealer Wins!";

    } else {
        return "It is a Draw!"         }}

in main():

Console.WriteLine(DisplayResult(playerscore,dealerscore));
Console.ReadLine();
RadioSpace
  • 953
  • 7
  • 15