C++ Program to pass an array to the function


In this program, You will learn how to pass an array to the function in C++.


Pass array to the function in C++

printArray(arr,size);


Example: How to pass an array to the function in C++

Run it Now
#include<iostream>
using namespace std;

void printArray(int arr[], int n) {

    cout << "\nArray list is :";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << "\t";
    }
}

int main() {

    int arr[10], i, n;
    cout << "Enter Size of an Array :";
    cin>>n;

    cout << "\nEnter Array elemensts :";
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }

    printArray(arr, n);

    return 0;
}

Output:

Enter Size of an Array :6
Enter Array elemensts :2 3 44 55 66 77
Array list is :2 3	44 55 66 77