Functions

Masa mula-mula kerja dalam bidang pengaturcaraan sistem saya suka sangat buat code panjang dan berulang kali dalam satu method. Apabila rakan-rakan lain ingin membuat penambahbaikan atau penyelenggaraan agak sukar bagi mereka kerana memerlukan masa yang lama.

Syukur Alhamdulillah, rezeki saya pada waktu itu ada kawan-kawan sekerja yang prihatin dengan cara penulisan code saya dan memberi teguran serta mengajarkan cara-cara menulis code yang pendek dan boleh dipanggil banyak kali sesuatu function yang sama dalam banyak method.

Untuk pengetahuan semua, sehingga ke hari ini saya masih menggunapakai cara tersebut dan kongsikan dengan rakan-rakan yang lain. Suatu ilmu yang amat2 berharga.

Ini antara yang saya dapat kongsikan dan untuk rujukan saya. Saya rujuk di link https://csharp.net-tutorials.com/basics/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 ourselves, 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.

Ia memang sangat2 memudahkan cara pengaturcaraan code dan memudahkan saya membuat penyelenggaraan atau penambahbaikan serta menjimatkan masa saya juga.