In this article, we will learn about different ways to pass parameters to a functions in C#.
In C#, value-type parameters are that pass a copy of original value to the function rather than reference. It does not modify the original value. A change made in passed value does not alter the actual value. In the following example, we have pass value during function call.
Example:
using System;
namespace CallByValue {
class Program {
// User defined function
public static void Show(int val) {
val *= val; // Manipulating value
Console.WriteLine("Value inside the show function " + val);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args) {
int val = 50;
Console.WriteLine("Value before calling the function " + val);
Show(val); // Calling Function by passing value
Console.WriteLine("Value after calling the function " + val);
}
}
}
Output:
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 50
C# provides a ref keyword to pass argument as reference-type. It passes reference of arguments to the function rather than copy of original value. The changes in passed values are permanent and modify the original variable value.
Example:
using System;
namespace CallByReference {
class Program {
// User defined function
public static void Show(ref int val) {
val *= val; // Manipulating value
Console.WriteLine("Value inside the show function " + val);
// No return statement
}
// Main function, execution entry point of the program
static void Main(string[] args) {
int val = 50;
Program program = new Program(); // Creating Object
Console.WriteLine("Value before calling the function " + val);
Show(ref val); // Calling Function by passing reference
Console.WriteLine("Value after calling the function " + val);
}
}
}
Output:
Value before calling the function 50
Value inside the show function 2500
Value after calling the function 2500
C# provides out keyword to pass arguments as out-type. It is like reference-type, except that it does not require variable to initialize before passing. We must use out keyword to pass argument as out-type. It is useful when we want a function to return multiple values.
Example 1:
using System;
namespace OutParameter {
class Program {
// User defined function
public static void Show(out int val) // Out parameter
{
int square = 5;
val = square;
val *= val; // Manipulating value
}
// Main function, execution entry point of the program
static void Main(string[] args) {
int val = 50;
Console.WriteLine("Value before passing out variable " + val);
Show(out val); // Passing out argument
Console.WriteLine("Value after recieving the out variable " + val);
}
}
}
Output:
Value before passing out variable 50
Value after receiving the out variable 25
The following example demonstrates that how a function can return multiple values.
Example 2:
using System;
namespace OutParameter {
class Program {
// User defined function
public static void Show(out int a, out int b) // Out parameter
{
int square = 5;
a = square;
b = square;
// Manipulating value
a *= a;
b *= b;
}
// Main function, execution entry point of the program
static void Main(string[] args) {
int val1 = 50, val2 = 100;
Console.WriteLine("Value before passing \n val1 = " + val1 + " \n val2 = " + val2);
Show(out val1, out val2); // Passing out argument
Console.WriteLine("Value after passing \n val1 = " + val1 + " \n val2 = " + val2);
}
}
}
Output:
Value before passing
val1 = 50
val2 = 100
Value after passing
val1 = 25
val2 = 25
In the editor below, perform the different tasks as directed.
Create two different functions:
Then, compare if the both the values after passing them to the functions are equal or not.
If equal, print True, else print False.
</p>