×

C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on them which can be reused. Let’s see an example of passing array to the function.
using System;  
public class ArrayExample  
{  
static void printArray(int[] arr)  
{  
Console.WriteLine("Printing array elements:");  
for (int i = 0; i < arr.Length; i++)  
{  
Console.WriteLine(arr[i]);  
}  
}  
public static void Main(string[] args)  
{  
int[] arr1 = { 25, 10, 20, 15, 40, 50 };  
int[] arr2 = { 12, 23, 44, 11, 54 };  
printArray(arr1);
            printArray(arr2);  
}  
}
Output
Printing array elements:
25
10
20
15
40
50
Printing array elements:
12
23
44
11
54
Passing Array to function: finding second largest element of array
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[10];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        int m=secondLargest(arr);
                        Console.WriteLine("Second largest element in the array is "+m);
            }
            static int secondLargest(int[] arr)
            {
                        int largest=arr[0];
                        int second_largest=arr[0];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    if(arr[i]>largest)
                                    {
                                                largest=arr[i];
                                    }
                        }
                        for(int i=1;i<arr.Length;i++)
                        {
                                    if(arr[i]>second_largest && arr[i]<largest)
                                                second_largest=arr[i];
                        }                                          
                        return second_largest;
            }
}
Output
10
20
30
40
50
60
70
80
80
90
Second largest element in the array is 80  
Passing Array to function Example: sorting the array using bubble sort
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[10];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        sort(arr);
            }
            static void sort(int[] arr)
            {
                        int temp;
                        Console.WriteLine("The Sorted array is... ");
                        for(int i=0;i<arr.Length;i++)
                        {
                                    for(int j=i+1;j<arr.Length;j++)
                                    {
                                                if(arr[j]<arr[i])
                                                {
                                                            temp=arr[i];
                                                            arr[i]=arr[j];
                                                            arr[j]=temp;
                                                }
                                    }
                        }
                        for(int i=0;i<arr.Length;i++)
                        {
                                    Console.WriteLine(arr[i]);
                        }
            }
}
Output
10
12
3
2
6
1
7
3
2
1
The Sorted array is... 
1
1
2
2
3
3
6
7
10
12
Passing Array to function: printing sum and average of all elements
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[5];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        sum(arr);
            }
            static void sum(int[] arr)
            {
                        int sum=0;
                        float avg=0;
                        for(int i=0;i<arr.Length;i++)
                        {
                                    sum=sum+arr[i];
                        }
                        avg=sum/5;
                        Console.WriteLine("sum of the array is "+sum+" Average of the array is "+avg);
            }
}
Output
1
2
3
4
5
sum of the array is 15 Average of the array is 3

Related Topics

Ref and Out in C#

 Ref in c# The ref is keyword that describes that any value that passes through the reference. We can use the ref keyword in 4 ways; In a programming code, in that...

4 minutes read.

C# Polymorphism

Polymorphism is the combination of two words, poly+forms which means many forms. It is a greek word . In C#, Polymorphism is achieved by mixing three mechanisms that are Inheritance,...

3 minutes read.

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# static field

In C#, static filed is the one which belongs to the class not instance hence we don’t need to create instance to access static field. The memory is assigned only...

5 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Params

There are cases when we don’t know the actual number of parameters in the function call. Well, in C# we can pass any number of parameters in the function call...

3 minutes read.

C# Jagged Arrays

C# provides the concept of jagged array which means array of arrays. The elements of jagged array are also arrays. The element size of jagged array can vary.C# Jagged Arrays...

3 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# If Statements

In C#, If Statements are used to check the condition. Expression given in if statement is evaluated, If it is true then if block gets executed if it is false...

2 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Object and classes

As we know, C# is an object oriented programming language, it provides oops concepts which are relevant to real world problems. C# Object: An object is a real world entity. It...

6 minutes read.

C# Data Types

A Data type is the type of data which the variable can store such as integer, float, double, character, etc. In C#, Data types are divided into three parts 1. Value data type 2....

2 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# Multidimensional Arrays

In C#, A multidimensional Array represents data storage in tabular form (in the form of matrix (rows and columns)). Number of dimensions is not fixed. It can be 2, 3...

1 minute read.

C# functions with out variable

In C#, the out variable can be used like a reference variable. We don't need to initialize the variable before passing it into a function call as a out-type. Using...

1 minute read.

C# Interface

Interface can be defined as a blueprint of the class. It can only have abstract methods. It has to be implemented by a class or struct. It is mainly used...

1 minute read.