Parameters and Arguments
[print-me]
Parameters give a method something to play with. They are like raw information that the method will process to give you the data you are looking for. Imagine parameters as information you give to an employer that he needs in order to finish his job. Consider a method that calculates the sum of two values for example. In order for that method to be flexible and useful, parameters are needed. A method can have any number of parameters. Each parameter can have different data types. Below shows the syntax of a method with N parameters.
returnType MethodName(datatype param1, datatype param2, ... datatype paramN)
{
code to execute;
}
The parameters are placed between the parentheses following the MethodName. You can have as many parameters as you want as long as they are essential for the job of the method. When calling the method, you must provide the arguments. The arguments are the values that will be assigned to each of the parameters. The order is important when passing arguments in a method. Failure to follow the right order or sequence of providing the arguments can lead to logic and runtime errors. Let’s demonstrate all of this by looking at an example.
using System;
namespace ParametersDemo
{
public class Program
{
static int CalculateSum(int number1, int number2)
{
return number1 + number2;
}
public static void Main()
{
int num1, num2;
Console.Write("Enter the first number: ");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the second number: ");
num2 = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Sum = {0}", CalculateSum(num1, num2));
}
}
}
Example 1 – Methods with Parameters
Enter the first number: 10 Enter the second number: 5 Sum = 15
The program in Example 1 declared a method named CalculateSum (lines 7-10) which calculates the sum of two values provided by the user. The method has two parameters which are the numbers to be added. The method returns the sum of two integer values therefore we indicate the return type as int to be added. Note at the data types of the parameters. The first parameter named number1 is expecting an int value and the second parameter named number2 is expecting also an int value. In the body, the return statement returns the result of the expression that adds the two values.
Inside method Main, the program asks the user for two values and assign them inside variables. We then call the method providing the two arguments inside the parentheses. The value of num1 will be passed to the first parameter of the method and the value of num2 will be passed to the second parameter of the method. Right now, interchanging the position of the two values when calling the method has no effect since addition is commutative. Just keep in mind that the position of the arguments when calling a method must exactly match the order of the parameters in the method declaration. After passing the values 10 and 5 to the method parameters, the parameters now has those values. Note that parameter names, by convention, uses camelCasing. Inside the method body, line 9 adds the two values using an addition expression and the result is returned to the caller. Last lesson, we used a variable to hold the results, but as you can see now, we can simply provide the expression and return the results immediately.
Inside the Main() method, we ask the user of two numbers that will be added together. Line 21 calls the CalculateSum() method and we passed the two integer values given by the user. The two integer is added inside the method and their sum is returned to the caller. The returned value is then displayed by the Console.WriteLine() method.
Let’s look at another program but this time, we will be using two parameters with different data types.
using System;
namespace ParametersDemo2
{
public class Program
{
static void ShowMessageAndNumber(string message, int number)
{
Console.WriteLine(message);
Console.WriteLine("Number = {0}", number);
}
public static void Main()
{
ShowMessageAndNumber("Hello World!", 100);
}
}
}
Example 2 – Another Example of Using Parameters
Hello World! Number = 100
Example 2 shows a method (lines 7-11) accepting a string value for the first parameter and an int for the second. The method simply displays the two values passed to it. Line 15 calls the method we created. We specified a string message first and then the number. If, for example, we called the method like this:
ShowMessageAndNumber(100, "Welcome to Visual C# Tutorials!");
The above statement will produce an error since 100 will be passed to the string parameter and the message “Hello World!” will be passed to the int parameter. This shows that the order of the arguments when calling the method is very important. Consider the program in Example 1. It required us to pass 2 numbers of int type. If that is the case, we can supply the values in any order. But if the method parameters have specific purposes, the order must be considered.
void ShowPersonStats(int age, int height)
{
Console.WriteLine("Age = {0}", age);
Console.WriteLine("Height = {0}", height);
}
//Using the proper order of arguments
ShowPersonStats(20, 160);
//Acceptable, but produces odd results
ShowPersonStats(160, 20);
The example above shows that even if the method accepts two parameters of the same data type, each parameter has a specific purpose. The first call is valid because the age of the person will be 20 and his/her height will be 160cm. If we are to switch the positions of the arguments, the person’s age will be 160 and his/her height will be 20cm which is not so realistic.
Knowing the principle of returning values and passing arguments, you do many complex things. The following code snippet shows you that you even pass a returned value of a method as an argument to another method.
int MyMethod()
{
return 5;
}
void AnotherMethod(int number)
{
Console.WriteLine(number);
}
// Codes skipped for demonstration
AnotherMethod(MyMethod());
The above code will have an output of 5 since MyMethod() returned 5 and that value was passed as an argument to the AnotherMethod(). Again, mastering the concept of returning and passing values will allow you to make more complex codes.