OK. I get it that to return multiple variables from a function is a dead end. And to use global variables is simply too messy when the program grows bigger. And if I use pointers, well, god knows whether the memory space the functions created still exists when the "return" is performed. And so, I really have no idea how to pass an array back with a "return". Any one has a better idea?
Thanks very much for the help...
#include <iostream>
#include <ctime>
usingnamespace std;
#define ARRAYSIZE 10
int change_array( int *, int );
int main( int arc, char *argv[] )
{
int array[ARRAYSIZE];
int recvresult;
cout << "Array before function call" << endl;
for( int i = 0; i < ARRAYSIZE; i++ )
cout << array[i] << endl;
recvresult = change_array( array, ARRAYSIZE );
cout << endl;
cout << "Array after funtion call" << endl;
for( int i = 0; i < ARRAYSIZE; i++ )
cout << array[i] << endl;
cin.get();
}
int change_array( int *ar1, int ars )
{
srand( time(NULL ));
for( int i = 0; i < ars; i++ )
{
ar1[i] = rand();
}
returntrue;
}
create your own array class
or go to http://sourceforge.net/projects/ and search for some Matrix C++ Library have a look at their documentation first, than see which one is better has documented and is easy for you. Download it and learn from their codes and programming techniques.
@therockon7throw would you recommend writing your own std::cout before learning how to use the one provided by the standard library? First learn how to use basic C++, then how to extend it.
You can return an STL container from a function but if it is big then there will be a lot of copying going on.
Another option is to pass a non-const reference to the container into the function and then fill the the container in the function.
A third option is to use a reference counted object to avoid the deep copying going on. Either make this yourself or use a smart pointer such as boot::shared_ptr
You can return an STL container from a function but if it is big then there will be a lot of copying going on.
Most compilers today will do RVO to avoid the copying. In C++11 if you return a local vector, it is guaranteed that there will be no copying of the elements.