Array to Functions

This article shows that, functions can be used to modify the elements of the array. Arrays are passed by value to the function. This is possible because the value of the array variable is the address of the variable.

This example shows two function add and display. add function inserts the elements in the array and display function shows the elements of the array.

#include<iostream>
using namespace std;
void add(int a[],int size)
{
	for(int i=0;i<size;i++){
		cout<<"a["<<i<<"] : ";
		cin>>a[i];
	}
}
void display(int a[],int size){
	cout<<"The elements are ";
	for(int i=0;i<size;i++){
		cout<<a[i]<<" ";
	}
	cout<<endl;
}
int main(){
	int size;
	cout<<"Enter size of array : ";
	cin>>size;
	int a[size];
	add(a,size);
	display(a,size);
	return 0;
}
output of the above program. array to functions
output of the above program. array to functions

One Reply to “Array to Functions”

Leave a Reply

Your email address will not be published. Required fields are marked *