Wednesday, 7 December 2016

11 C# Functions

Functions

A function allows you to encapsulate a piece of code and call it from other parts of your code. You may very soon run into a situation where you need to repeat a piece of code, from multiple places, and this is where functions come in. In C#, they are basically declared like this:
<visibility> <return type> <name>(<parameters>)
{
 <function code>
}
To call a function, you simply write its name, an open parenthesis, then parameters, if any, and then a closing parenthesis, like this:
DoStuff();
Here is an example of our DoStuff() function:
public void DoStuff()
{
    Console.WriteLine("I'm doing something...");
}
The first part, public, is the visibility, and is optional. If you don't define any, then the function will be private. More about that later on. 
Next is the type to return. It could be any valid type in C#, or as we have done it here, void. A void means that this function returns absolutely nothing. Also, this function takes no parameters, as you can see from the empty set of parentheses, so it's actually just a tad bit boring. Let's change that:
public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    return result;
}
We've changed almost everything. The function now returns an integer, it takes two parameters (both integers), and instead of outputting something, it makes a calculation and then returns the result. This means that we can add two numbers from various places in our code, simply by calling this function, instead of having to write the calculation code each time. While we don't save that much time and effort in this small example, you better believe that you will learn to love functions, the more you use C#. This function is called like this:
int result = AddNumbers(10, 5);
Console.WriteLine(result);
As mentioned, this function actually returns something, and it has to, because we told C# that it's supposed to do so. When declaring anything else than void as a return type, we are forcing our self to return something. You can try removing the return line from the example above, and see the compiler complain: 

'AddNumbers(int, int)': not all code paths return a value 

The compiler is reminding us that we have a function which doesn't return something, although we promised. And the compiler is pretty clever! Instead of removing the line, try something like this:
public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    if(result > 10)
    {
        return result;
    }
}
You will see the exact same error - but why? Because there is no guarantee that our if statement will evaluate to true and the return line being executed. You can solve this by having a second, default like return statement in the end:
public int AddNumbers(int number1, int number2)
{
    int result = number1 + number2;
    if(result > 10)
    {
        return result;
    }
    return 0;
}
This will fix the problem we created for our self, and it will also show you that we can have more than one return statement in our function. As soon as a return statement is reached, the function is left and no more code in it is executed. In this case, it means that as long as the result is higher than 10, the "return 0" is never reached.

No comments:

Post a Comment