12 years, 5 months ago.

how do i give arrays along with a function?

i have an array of 4 values, i want to take this array along in my function and do something with the value there in the beginning of the programming, what about declaration function?

int calculate(int []);

int main()
{
int values[4] = {0, 1 , 2, 3};
int answer;
answer = calculate(values);

}

int calculate(int values)
{
....
return 5;
}

thanx in advance!!

1 Answer

12 years, 5 months ago.

Have a look at arrays in C++ in general: http://www.cplusplus.com/doc/tutorial/arrays/

Anyway in principe your code should work if you add '[]' after values in your function, or put '*' in front of it. After that you can simply do values[3]=2; for example. Do take into account C(++) doesnt know how long your array is, if you write values[10] = 0;, it will simply clear out a bunch of bytes in a certain location, which is not what you want.

Accepted Answer