#include<iostream>
using namespace std;

void getdata (int a[], int size);

int main ()
{

int size=10;
int a[size];
getdata (a[], size);

    return 0;
}

void getdata (int a[], int size)
{
    for (int i=0; i<size; i++)
        cin>>a[i];
}

Also, please provide the purpose of this program. I assume you want your function to populate the array. Sure, this may happen in the function, but you aren't returning the edited variable nor are you passing it by reference.

Also, please provide the purpose of this program.

It's pretty obvious that the purpose is to populate an array from a function.

but you aren't returning the edited variable nor are you passing it by reference.

Arrays are passed by simulated reference (ie. by pointer), so there's nothing wrong there. The two issues are thus:

  1. Semantic error: size is not a compile-time constant, and C++ doesn't allow array sizes that are not compile-time constants.

  2. Syntax error: The subscript operator is not used when passing an array to a function, just the array name.

The corrections are simple and obvious, provided the OP doesn't want a dynamically sized array:

#include <iostream>

using namespace std;

void getdata(int a[], int size);

int main()
{
    const int size = 10;

    int a[size];

    getdata(a, size);

    return 0;
}

void getdata(int a[], int size)
{
    for (int i = 0; i < size; i++)
        cin >> a[i];
}

You might say that failing to use the populated array is an error as well, since the only way to confirm the code works is by stepping through it in a debugger.

This article has been dead for over six months. Start a new discussion instead.