What is the difference between passing arrays to function and declaring array inside main? What are the advantages to doing it with each structure? For example, in the first code sample I pass the array into a function, and in the second I declare it inside main. What's the difference?
Version 1:
#include <iostream>
using namespace std;
void printArray(int theArray[], int sizeofArray);
//passive arrays to function..
int main() {
int arr[3] = {44,23,22};
int arry[5] = {56,23,11,23,55};
printArray(arr, 3);
}
void printArray(int theArray[], int sizeofArray){
for(int x =0;x<sizeofArray; x++){
cout << theArray[x] << endl;
}
}
Version 2:
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int arr[3] = {44,23,22};
int arry[5] = {56,23,11,23,55};
for(int x=0;x<3;x++){
cout << arr[x] << endl;
}
}