Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C# - METHOD ARGUMENTS


Say, you want to pass some information to a Method.


For example, if you want a Method to add two numbers. You need to pass those two numbers to the Method.


Say, in the below example, you want the add Method to add two numbers for you. And you have passed those numbers in between the brackets () of the add Method(i.e. firstNumber and secondNumber).


And those elements inside the brackets () of the add Method is called as Parameters and the actual values passed to them are called as Arguments.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int firstNum = 5;
        int secondNum = 8;
        int addedResult;

        addedResult = add(firstNum,secondNum);

        System.Console.WriteLine("The added value is : "+addedResult);
    }

    //Add method starts here.
    static int add(int firstNumber, int secondNumber) 
    {
        int result;
        result = firstNumber + secondNumber;
        return result;
    }
}


Output :



  The added result is : 13

So, the variables inside the add() Method is called as Parameter.

C_Sharp

And the actual values (i.e. 5 and 8) to be sent to the Method are the Arguments.


Throughout the tutorial we will be calling the Parameters as Arguments to avoid unnecessary confusion.


Now, what if, you have defined a Method with two Argument but made the Method call with one argument.


What if there is an Argument mismatch?


Now, what if, you have defined a Method with two Argument but made the Method call with one argument.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int firstNum = 5;
        int secondNum = 8;
        int addedResult;

        addedResult = add(firstNum,secondNum);

        System.Console.WriteLine("The added value is : "+addedResult);
    }

    //Add method starts here.
    static int add(int firstNumber, int secondNumber, int thirdNumber) 
    {
        int result;
        result = firstNumber + secondNumber + thirdNumber;
        return result;
    }
}


Output :



  There is no argument given that corresponds to the required formal parameter 'thirdNumber' of 'MyApplication.add(int, int, int)'

So, in the above code we have the add Method with three Arguments.


static int add(int firstNumber, int secondNumber, int thirdNumber)
C_Sharp


But when the add Method is called, it is called with two Arguments only.

C_Sharp

And there is a mismatch of Arguments. And we end up with the below error.


There is no argument given that corresponds to the required formal parameter 'thirdNumber' of 'MyApplication.add(int, int, int)'

Let us see in the next example, how to fix it.


Fixing Argument mismatch with default arguments in function definition


To solve the problem of Argument mismatch, we use default arguments in function definition.


Let us see in the below example.


Example :



public class MyApplication
{
    public static void Main(string[] args)
    {
        int firstNum = 5;
        int secondNum = 8;
        int addedResult;

        addedResult = add(firstNum,secondNum);

        System.Console.WriteLine("The added value is : "+addedResult);
    }

    //Add method starts here.
    static int add(int firstNumber, int secondNumber, int thirdNumber = 11) 
    {
        int result;
        result = firstNumber + secondNumber + thirdNumber;
        return result;
    }
}


Output :



  The added result is : 24

So, in the previous example, we got the error as,


There is no argument given that corresponds to the required formal parameter 'thirdNumber' of 'MyApplication.add(int, int, int)'

And, to avoid the Argument mismatch problem, we have used default arguments in function definition.


Placing default arguments in function definition.


static int add(int firstNumber, int secondNumber, int thirdNumber = 11)
C_Sharp


So, we have placed the default value, 11 for the third argument


Now, although the function is called with two parameters,


addedResult = add(firstNum,secondNum);

With values, 5 and 8.


int firstNum = 5;
int secondNum = 8;
C_Sharp

C_Sharp

The function add() is called. And firstNum is passed to firstNumber and secondNum to secondNumber.


So, what happens to the third argument?


Well! It gets initialised with the default value 11.

C_Sharp