Why do we need method in any programming language?
Published: 2023-07-30 11:40:13

**What is method?**

Methods is exists almost all programming language. It prevents code duplication and makes program code more readable. There are two most important point for methods - input parameter and return type. A method may have a sequence of parameters. Parameters define the set of arguments that must be provided for that method. We will use C# syntax for examples. For example


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m31l36b6z9jn75rt31sp.PNG)

1. It is access specifiers. It define method accessibility
2. Return type
3. It is method name that you can give any name
4. These are input parameter list


In the following example, the method Foo has a single parameter named p, of type int

```
Foo (8); // 8 is an argument
static void Foo (int p) {...} // p is a parameter
```



Methods can declare optional parameters. A parameter is optional if it specifies a default value in its declaration. You can omit optional parameters when calling the method:

```
void Foo (int x = 23) 
{ 
    Console.WriteLine (x); 
}
Foo(); // 23
Foo(12); // 12
```

Rather than identifying an argument by position, you can identify an argument by name. It is called Named arguments. Named arguments can occur in any order. The following calls to Foo are semantically identical

```
void Foo (int x, int y) 
{ 
    Console.WriteLine (x + ", " + y); 
}

Foo (x:1, y:2);
Foo (y:2, x:1);
```

**Why do we need method?**

Let's approach to this question over simple task. Let's suppose that two numbers which are integers were given. We need to check which one of is bigger? If first number is bigger than second one, we should calculate multiplication between this numbers and sum of digits of same numbers. If second number is bigger than first one or they are equal, we should calculate addition between this numbers and sum of digits of same numbers. At last compare results. Which one of is bigger? For example:

12 and 23

- 12 is not bigger than 23 then
- 23 is bigger than 12 then
     12 + (1 + 2) = 15 and 23 + (2 + 3) = 28
- 15 and 28 => Result: 28 (bigger than 15)

Let's write this algorithm without method in C#:


```
int firstNumber = int.Parse(Console.ReadLine());
int secondNumber = int.Parse(Console.ReadLine());

if (firstNumber > secondNumber)
{
	// Find sum of digits of first number
    int sumOfFirstNumber = 0;
    int firstTempNumber = firstNumber;

    while (firstTempNumber > 0)
	{
        int lastDigit = firstTempNumber % 10;
		sumOfFirstNumber += lastDigit;
        firstTempNumber /= 10;
	}

    // Find sum of digits of second number
    int sumOfSecondNumber = 0;
    int secondTempNumber = secondNumber;

    while (secondTempNumber > 0)
    {
        int lastDigit = secondTempNumber % 10;
        sumOfSecondNumber += lastDigit;
        secondTempNumber /= 10;
    }

    int resultFirst = firstNumber * sumOfFirstNumber;
    int resultSecond = secondNumber * sumOfSecondNumber;

    if (resultFirst > resultSecond)
    {
        Console.WriteLine(resultFirst);
    }
    else
    {
        Console.WriteLine(resultSecond);
    }
}
else
{
    // Find sum of digits of first number
    int sumOfFirstNumber = 0;
    int firstTempNumber = firstNumber;

    while (firstTempNumber > 0)
    {
        int lastDigit = firstTempNumber % 10;
        sumOfFirstNumber += lastDigit;
        firstTempNumber /= 10;
    }

    // Find sum of digits of second number
    int sumOfSecondNumber = 0;
    int secondTempNumber = secondNumber;

    while (secondTempNumber > 0)
    {
        int lastDigit = secondTempNumber % 10;
        sumOfSecondNumber += lastDigit;
        secondTempNumber /= 10;
    }

    int resultFirst = firstNumber + sumOfFirstNumber;
    int resultSecond = secondNumber + sumOfSecondNumber;

    if (resultFirst > resultSecond)
    {
        Console.WriteLine(resultFirst);
    }
    else
    {
        Console.WriteLine(resultSecond);
    }
}
```


Now, let's try to make more simple via using method. Firstly, create 2 methods which return sum of digits of number and compare 2 numbers and print screen to max one.

```
int FindSumOfDigitsOfNumber(int number)
{
    int result = 0;

    while (number > 0)
    {
        int lastDigit = number % 10;
        result += lastDigit;
        number /= 10;
    }

    return result;
}

void FindMaxNumberAndPrint(int first, int second)
{
    if (first > second)
    {
        Console.WriteLine(first);
    }
    else
    {
        Console.WriteLine(second);
    }
}

```

Then we can write our main code as following:

```
int firstNumber = int.Parse(Console.ReadLine());
int secondNumber = int.Parse(Console.ReadLine());

if (firstNumber > secondNumber)
{
    // Find sum of digits of first number
    int sumOfFirstNumber = FindSumOfDigitsOfNumber(firstNumber);

    // Find sum of digits of second number
    int sumOfSecondNumber = FindSumOfDigitsOfNumber(secondNumber);

    int resultFirst = firstNumber * sumOfFirstNumber;
    int resultSecond = secondNumber * sumOfSecondNumber;

    FindMaxNumberAndPrint(resultFirst, resultSecond);
}
else
{
    // Find sum of digits of first number
    int sumOfFirstNumber = FindSumOfDigitsOfNumber(firstNumber);

    // Find sum of digits of second number
    int sumOfSecondNumber = FindSumOfDigitsOfNumber(secondNumber);

    int resultFirst = firstNumber + sumOfFirstNumber;
    int resultSecond = secondNumber + sumOfSecondNumber;

    FindMaxNumberAndPrint(resultFirst, resultSecond);
}
```

This is simple example and if you are software developer, you will face much more difficult tasks than this one during working. Every time when you use method, it will make your code more readable and short.

Best regards...