Summary: in this tutorial, you’ll learn how to pass an array to a function as an argument.
Passing an array to a function as an argument
To pass an array to a function, you specify the array parameter with the following syntax:
type [] arrayParameter
Code language: C# (cs)
The following illustrates how to pass an array to a function:
void PrintArray(decimal[] elements)
{
foreach (var element in elements)
{
Console.WriteLine($"{element:0.##}");
}
}
decimal[] salaries = { 100000, 120000, 150000 };
PrintArray(salaries);
Code language: C# (cs)
Output:
100000
120000
150000
Code language: C# (cs)
How it works.
First, define a function called PrintArray()
that outputs the elements of an array to the console:
void PrintArray(decimal[] elements)
{
foreach (var element in elements)
{
Console.WriteLine($"{element:0.##}");
}
}
Code language: C# (cs)
In this PrintArray()
function, we use the foreach
statement to iterate over the elements of the array and output each of them to the console.
Second, declare a decimal array and initialize it with some values:
decimal[] salaries = { 100000, 120000, 150000 };
Code language: C# (cs)
Third, pass the salaries array to the PrintArray()
function:
PrintArray(salaries);
Code language: C# (cs)
Note that you can initialize and pass a new array to the function in one step like this:
PrintArray(new decimal[]{ 100000, 120000, 150000 });
Code language: C# (cs)
Changing the array elements
Because arrays are reference types, the function can change the values of their elements. For example:
void PrintArray(decimal[] elements)
{
foreach (var element in elements)
{
Console.WriteLine($"{element:0.##}");
}
}
void Increase(decimal[] salaries, decimal percentage = 0.05m)
{
for (int i = 0; i < salaries.Length; i++)
{
salaries[i] = salaries[i] * (1 + percentage);
}
}
decimal[] salaries = { 100000, 120000, 150000 };
Console.WriteLine("Before increment:");
PrintArray(salaries);
Increase(salaries);
Console.WriteLine("After increment:");
PrintArray(salaries);
Code language: C# (cs)
Output:
Before increment:
100000
120000
150000
After increment:
105000
126000
157500
Code language: plaintext (plaintext)
How it works.
First, define the Increase()
function that increases the elements of salaries
array by a percentage. By default, the percentage is 5% (or 0.05):
void Increase(decimal[] salaries, decimal percentage = 0.05m)
{
for (int i = 0; i < salaries.Length; i++)
{
salaries[i] = salaries[i] * (1 + percentage);
}
}
Code language: C# (cs)
Next, declare and initialize an array of salaries:
decimal[] salaries = { 100000, 120000, 150000 };
Code language: C# (cs)
Then, output the elements of the salaries
array using the PrintArray()
function:
Console.WriteLine("Before increment:");
PrintArray(salaries);
Code language: C# (cs)
After that, pass the salaries
array to the Increase()
function:
Increase(salaries);
Code language: C# (cs)
Finally, output the elements of the salaries
array to the console:
Console.WriteLine("After increment:");
PrintArray(salaries);
Code language: C# (cs)
Summary
- Arrays are reference types, functions can change the values of the array elements.