Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How do i make a function return an array?

  1. Dec 3, 2009 #1
    in the c++ programming language, how do i make a function return an array?
     
  2. jcsd
  3. Dec 3, 2009 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    return array;

    But you probably need a little more detail
    It's more common to pass the address of the array to the function and let the function modify it directly.
     
  4. Dec 3, 2009 #3

    jtbell

    User Avatar

    Staff: Mentor

    If you declare an array locally inside a function, and then return it, you're actually returning a pointer to the data which was allocated inside the function. The problem is, when the function terminates, its local variables become undefined! More precisely, their memory is released and is likely to be re-used for local variables in the next function that you call.

    If you want/need to stick with arrays, there are two ways around this:

    1. Declare the array in the calling function, and pass a pointer to the function. Then you can either return the pointer,

    Code (Text):

    #include <iostream>
    #include <cmath>

    using namespace std;

    double *roots (double *x, int n)
    {
        for (int k = 0; k < n; ++k)
            x[k] = sqrt(k);
        return x;
    }

    int main ()
    {
        double numbers[5];
        double *sqrts;
        sqrts = roots (numbers, 5);
        for (int k = 0; k < 5; ++k)
            cout << sqrts[k] << endl;
        return 0;
    }
     
    or simply use the pointer parameter in the calling function, after the function call:

    Code (Text):

    #include <iostream>
    #include <cmath>

    using namespace std;

    void roots (double *x, int n)
    {
        for (int k = 0; k < n; ++k)
            x[k] = sqrt(k);
    }

    int main ()
    {
        double numbers[5];
        roots (numbers, 5);
        for (int k = 0; k < 5; ++k)
            cout << numbers[k] << endl;
        return 0;
    }
     
    2. Allocate the memory for the array dynamically using 'new' inside the function. Then it stays valid after the function terminates, and you can safely return a pointer to it. But you need to remember to 'delete' that memory at some point, in general, or your program is likely to spring a memory leak.

    Code (Text):

    #include <iostream>
    #include <cmath>

    using namespace std;

    double *roots (int n)
    {
        double *x = new double[n];
        for (int k = 0; k < n; ++k)
            x[k] = sqrt(k);
        return x;
    }

    int main ()
    {
        double *sqrts;
        sqrts = roots (5);
        for (int k = 0; k < 5; ++k)
            cout << sqrts[k] << endl;
        delete[] sqrts;
        return 0;
    }
     
    Or you can use a vector instead. You can declare a vector inside a function, and return it in a "return" statement, which copies the vector to the calling function. The potential drawback here is the time it takes to copy the vector, if it's large or you have to do it many times in a loop.

    Code (Text):

    #include <iostream>
    #include <cmath>
    #include <vector>

    using namespace std;

    vector<double> roots (int n)
    {
        vector<double> x(n);
        for (int k = 0; k < n; ++k)
            x[k] = sqrt(k);
        return x;
    }

    int main ()
    {
        vector<double> sqrts;
        sqrts = roots (5);
        for (int k = 0; k < 5; ++k)
            cout << sqrts[k] << endl;
        return 0;
    }
     
     
    Last edited: Dec 3, 2009
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook