-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayParameterFunction.cpp
More file actions
47 lines (37 loc) · 901 Bytes
/
arrayParameterFunction.cpp
File metadata and controls
47 lines (37 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
Arrays are passed by reference to the functions along with their size.
*/
#include <iostream>
using namespace std;
// Function prototypes
void inc(int array[], int size);
void print(int array[], int size);
// Test Driver
int main() {
int a1[] = { 8, 4, 5, 3, 2 };
// Before increment
print(a1, 5); // {8,4,5,3,2}
// Do increment
inc(a1, 5); // Array is passed by reference (having side effect)
// After increment
print(a1, 5); // {9,5,6,4,3}
system("pause");
}
// Function definitions
// Increment each element of the given array
void inc(int array[], int size) { // array[] is not const
for (int i = 0; i < size; ++i) {
array[i]++; // side-effect
}
}
// Print the contents of the given array
void print(int array[], int size) {
cout << "{";
for (int i = 0; i < size; ++i) {
cout << array[i];
if (i < size - 1) {
cout << ",";
}
}
cout << "}" << endl;
}