Advertisement

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++

#include<iostream>
using namespace std;

void printArray(int arr[], int n) {
    cout << "Array list is:";
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
}

int main() {

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

    cout << "Enter array elements:";
    for (i = 0; i < n; i++) {
        cin >> arr[i];
    }

    printArray(arr, n);

    return 0;
}

Output:

Enter size of an array:5
Enter array elements:10 20 30 40 50
Array list is:10 20 30 40 50