Passing Arrays to Functions In C++
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
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming