Functions and Arrays in C++ with examples

2

We can pass Arrays to the Functions as parameters. When we pass parameter to the calling function then only the address of first index of the array is passed. One should always remember that the Arrays are always passed by reference not by value, which means when we pass it, the new separate copy of the actual parameters does not created, which means that both actual parameters and formal parameters points towards the same memory location. If we make changes in the formal parameters then it will also affect the actual parameters. In this article we further discussed functions and arrays in C++

Syntax for passing Arrays as a parameter to the Functions:-

The below syntax is used for passing array as a parameter to the function.

(Return type) (Name of Function) (Parameter[]);

For Example:-

Int Show (arr1[]);

The above statement will declare the function show which will accept the array as a parameter.

Calling a function with the Arrays as a Parameter:-

To pass an arrays as the parameter to the function, the name of array is passed as an actual parameter to the calling Function, the index of an array is not provided. The address of the first index of an array is passed to the called function. Then called function uses its address to perform operations.

Following example will illustrate how to pass an array to the function as a parameter.

For Example:-


#include<iostream.h>
#include<conio.h>
#include<string.h>

using namespace::std;

// The called function //
void rando1(int a[])
{
for(int j=0;j<5;j++)
{
cout<<"Your Entered array is :" ;
cout<<a[j];
cout<<endl;
}
}

main()
{
int arr[4];
for(int i=0;i<5;i++)
{
cout<<"Enter the array of 5 numbers :" ;
cin>>arr[i];
}
cout<<endl;

// The calling function //
rando1(arr);

getch();
}

Explanation:-

Functions and arrays C++

The above program will declare a function rando1, which can accept the array of integers as the parameter. Then the main function will declare an array, which will store five digits then it will passed to the rando1 function. The rando1 function will display the elements of this array. One thing should be noted, the identifiers ‘arr’ and ‘a’ refers to the same memory location.

Passing Individual Array elements to the Functions:-

An individual array element can be passed to the function. The data type of array and the data type of function parameter should be same. The individual element of the array is passed to the function just like the simple variable. Take an example of a function which is defined to take an integer parameter, this function will accept the individual element of the integer Array as it accepts the integer variable.

Following example will illustrate how to pass an array to the function as a parameter.

For Example:-


#include<iostream.h>
#include<conio.h>
#include<string.h>

using namespace::std;

// The called function //
void rando1(int a)
{
cout<<"Your Entered indvidual array element is :" ;
cout<<a;
cout<<endl;
}

main()
{
int arr[4];
for(int i=0;i<5;i++)
{
cout<<"Enter the array of 5 numbers :" ;
cin>>arr[i];
}
cout<<endl;

// The calling function //
for(int k=0;k<5;k++)
{
rando1(arr[k]);
}

getch();
}

Explanation:-

functions and arrays example

The above program will declare a function rando1, which can accept the integer variable as the parameter. Then the main function will declare an array, which will store five digits then its elements will passed to the rando1 function one by one by using a loop. Then rando1 function will display its elements individually on the screen.

Passing two-dimensional Array to the Function:-

Two-Dimensional Array could be passed to the function in a same way as the One-Dimensional array, but one should always remember that, first dimension is not necessary but the second dimension is required, Otherwise compiler will detect errors.

For example:-


Int rando1(int a[][]); //Error. The second dimension is necessary.
Int rando1(int a[][3]); // Ok.
Int rando1(int a[2][3]); // Ok.

The below example will illustrate how to pass two dimensional array to the function, also how to perform operations on it.


#include<iostream.h>
#include<conio.h>
#include<string.h>

using namespace::std;

// The called function //
void rando1(int a[3][2])

{
for(int w1=0;w1<3;w1++)
{
for(int w2=0;w2<2;w2++)
{
cout<<"Your Entered indvidual array element is :" ;
cout<<a[w1][w2];
cout<<endl;
}
}
}
main()
{
int arr[3][2];
for(int i=0;i<3;i++)
{
for( int j=0;j<2;j++)
{
cout<<"Enter the array of 4 numbers :" ;
cin>>arr[i][j];
}
}
cout<<endl;

// The calling function //

rando1(arr);

getch();
}

Explanation:-

C++ functions and arrays

The above program will declare a function rando1, which can accept the Two-Dimensional Array of integers as the parameter. Then the main function will declare a 2-D array, which will store six digits then it will passed to the rando1 function. It is noted that the first dimension is not necessary but second dimension is always required, then rando1 function will display the elements of the array.

[yop_poll id=”9″] If you like my article on functions and arrays and wanted to read my other articles then visit my Homepage.

@ 2014 HellGeeks.com

4.7/5 - (3 votes)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a reply