Passing Arrays to Functions In C++
On this page (7sections)
Overview
we can pass array parameter three types
- Sized Array Parameter
- Unsized Array Parameter
- Pointer Array Parameter
As working term above three types is same.
data_type[]
and
data_type[size] // here size is a compile-time number,which is array size.
//Are exactly the pointer reference also same as
data_type*
Syntax
// Type 1 : Sized Array Parameter
int sum1(int tmp[12]);
// Type 2 : Unsized Array Parameter
int sum2(int tmp[]);
// Type 3 : Pointer Reference Array Parameter
int sum3(int * tmp);
Passing Arrays to Functions example program In C++
/* Passing Arrays to Functions example program In C++ Programming Language
Array Example In C++*/
// Header Files
#include <iostream>
#include<conio.h>
using namespace std;
// Type 1 : Sized Array Parameter Function Declaration
int sum1(int tmp[12]);
// Type 2 : Unsized Array Parameter Function Declaration
int sum2(int tmp[]);
// Type 3 : Pointer Array Parameter Function Declaration
int sum3(int * tmp);
int main() {
int total = 0;
// declaring and Initializing sales array in C
int sales[12] = {100, 90, 80, 110, 115, 210, 80, 95, 125, 135, 140, 174};
int i; // Variable for access sales array index
cout << "Passing Arrays to Functions :Sales Sum Demo : In C++ Example Program\n";
total = sum1(sales);
cout << "\nType 1 : Sum :" << total;
total = sum2(sales);
cout << "\nType 2 : Sum :" << total;
total = sum3(sales);
cout << "\nType 3 : Sum :" << total;
getch();
return 0;
}
// Type 1 : Sized Array Parameter Function Definition
int sum1(int tmp[12]) {
int sum = 0, i;
for (i = 0; i < 12; i++) {
sum = sum + tmp[i];
}
return sum;
}
// Type 2 : Unsized Array Parameter Function Definition
int sum2(int tmp[]) {
int sum = 0, i;
for (i = 0; i < 12; i++) {
sum = sum + tmp[i];
}
return sum;
}
// Type 3 : Pointer Array Parameter Function Definition
int sum3(int * tmp) {
int sum = 0, i;
for (i = 0; i < 12; i++) {
sum = sum + tmp[i];
}
return sum;
}
Sample Output
Passing Arrays to Functions :Sales Sum Demo : In C++ Example Program
Type 1 : Sum :1454
Type 2 : Sum :1454
Type 3 : Sum :1454
How It Works
This C++ program demonstrates Passing Arrays to Functions. It first prepares the data it needs, then walks through the data with a loop to compute the result, and finally prints the output shown in the Sample Output above.
- Declare the variables that hold the program’s data.
- Iterate over the data using a loop to apply the logic.
- Print the final result to the console so you can compare it with the sample output.
Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.
Related Pages
Continue learning with these related tutorials and programs:
- C++ Tutorials — Browse all C++ Tutorials.
- Understanding Array in C++ — Tutorial — array declaration, access and iteration.
- Read user input into Array in C++ — More in array in c.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Passing Arrays to Functions, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data, arrays and user-defined functions, illustrating a common pattern in C++ programming.