Returning Values from functions

The simplest way to exchange data with a function is to use a return value. Functions that have return values evaluate to that value, in exactly the same way that variables evaluate to the values they contain when you use them in expressions. Just like variables, return values have a type.
 
For example, you might have a function called GetString() whose return value is a string. You could use this in code, such as the following:

string myString;
myString = GetString();

Alternatively, you might have a function called GetVal() that returns a double value, which you could use in a mathematical expression:

double myVal;
double multiplier = 5.3;
myVal = GetVal() * multiplier;

When a function returns a value, you have to modify your function in two ways:
1) Specify the type of the return value in the function declaration instead of using the void keyword.
2) Use the return keyword to end the function execution and transfer the return value to the calling code.
 
In code terms, this looks like the following in a console application function of the type you ’ ve been looking at:
static < returnType > < functionName > ()
{

return < returnValue > ;
}

The only limitation here is that < returnValue > must be a value that either is of type < returnType > or can be implicitly converted to that type. However, < returnType > can be any type you want, including the more complicated types you ’ ve seen. This might be as simple as the following:

static double GetVal()
{
    return 3.2;
}

However, return values are usually the result of some processing carried out by the function; the preceding could be achieved just as easily using a const variable.
 
When the return statement is reached, program execution returns to the calling code immediately. No lines of code after this statement are executed, although this doesn ’ t mean that return statements can only be placed on the last line of a function body. You can use return earlier in the code, perhaps after performing some branching logic. Placing return in a for loop, an if block, or any other structure causes the structure to terminate immediately and the function to terminate, as shown here:

static double GetVal()
{
     double checkVal;
     // CheckVal assigned a value through some logic (not shown here).
     if (checkVal < 5)
         return 4.7;
     return 3.2;
}

Here, one of two values may be returned, depending on the value of checkVal . The only restriction in this case is that a return statement must be processed before reaching the closing } of the function. The following is illegal:

static double GetVal()
{
    double checkVal;
    // CheckVal assigned a value through some logic.
    if (checkVal < 5)
        return 4.7;
}

If checkVal is > = 5 , then no return statement is met, which isn ’ t allowed. All processing paths must reach a return statement. In most cases, the compiler detects this and gives you the error ” not all code paths return a value. “.
 
As a final note, return can be used in functions that are declared using the void keyword (that don ’ t have a return value). In that case, the function simply terminates. When you use return in this way, it is an error to provide a return value in between the return keyword and the semicolon that follows.