2

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;
    }
}
durron597
  • 31,968
  • 17
  • 99
  • 158
  • You are declaring the array(s) inside of `main` in both examples. – juanchopanza Jul 30 '15 at 13:38
  • Is there any difference? between the two?? I mean, i don't know when to use them.. –  Jul 30 '15 at 13:39
  • I think there is no difference lol. Unless u want to reuse function printarray with different parameters. **In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. ** http://www.cplusplus.com/doc/tutorial/functions/ – Hani Goc Jul 30 '15 at 13:43
  • Version 1 code is reused !!! – Jeyaram Jul 30 '15 at 13:44
  • @HaniGoc , THANKS! I think i missed some parts of what the tutor is saying about functions. –  Jul 30 '15 at 13:46
  • The question seems more between using the array immediately and using it after passing to a function. If this is the case, then the difference is about array decaying as explained here: http://stackoverflow.com/questions/1461432/what-is-array-decaying – stefaanv Jul 30 '15 at 13:49
  • @stefaanv , yes this my problem too. thanks –  Jul 30 '15 at 13:50

1 Answers1

4

As pointed by Juanchopanza, in both cases your arrays are declared inside the main function.

The main difference is your code organization. The purpose of functions is to allow you to do a task on different parameters. In your second example, you can still do printArray(arry, 5) to print the content of your second array like so :

#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);
   printArray(arry, 5); // Let's see what's inside arry too !
}

void printArray(int theArray[], int sizeofArray){
    for(int x =0;x<sizeofArray; x++){
        cout << theArray[x] << endl;
    }
}

If you want to do this in your first example, you have to copy/paste the loop and it will result in a less readable code look how ugly it is :

#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;
    }
    for(int x=0;x<5;x++) {           // Here
        cout << arry[x] << endl;
    }
}

What would happen if you had to print ten different arrays ? Would you prefer to call ten times the function printArray with different arguments or would you copy/paste your loop ten times ?

Telokis
  • 3,399
  • 14
  • 36
  • 1
    As an aside, avoid `std::endl` unless you really need that manual flush. Also, `using namespace std` is considered really bad form and error-prone. – Deduplicator Jul 30 '15 at 13:49