TOC

This article is currently in the process of being translated into Arabic (~17% done).

الأساسيات:

المهام "Functions"

تسمح لك المهمة بجمع أسطر التعليمات واستدعائها من مكان آخر في البرمجية. قد تمر قريباً بحالة تحتاج فيها لتكرار بضعة أسطر برمجية وبعدة أماكن، وهنا يأتي دور المهام. في #C، يتم تعريف هذه المهام ببساطة بالشكل الآتي: -- القدرة على الاكتشاف - نوع النتيجة المرجوة من المهمة - اسم المهمة (معايير "parameters") { التعليمات }

<visibility> <return type> <name>(<parameters>)
{
<function code>
}

لاستدعاء مهمة ما، قم ببساطة بكتابة اسمها، وافتح قوساً وضع بداخله القيم المرجعة إن وجدت ثم أغلق القوس، كالتالي:

DoStuff();

وهنا مثال عن مهمة ()DoStuff:

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.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!