How do i make a function return an array?

In summary, in the C++ programming language, to make a function return an array, you can either pass the address of the array to the function and modify it directly, or allocate memory for the array dynamically using 'new' and remember to delete it later. Alternatively, you can use a vector instead, which can be declared and returned inside the function, but may have a longer execution time due to copying.
  • #1
okkvlt
53
0
in the c++ programming language, how do i make a function return an array?
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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:
#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:
#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:
#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:
#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:

Related to How do i make a function return an array?

1. How do I create a function that returns an array in JavaScript?

To create a function that returns an array in JavaScript, you can use the return keyword followed by the array you want to return. For example, function myFunction() { return [1, 2, 3]; } will return an array with the values 1, 2, and 3 when called.

2. Can a function return multiple arrays in JavaScript?

Yes, a function can return multiple arrays in JavaScript by using the return keyword followed by multiple arrays separated by commas. For example, function myFunction() { return [1, 2], [3, 4]; } will return two arrays with the values 1 and 2, and 3 and 4 respectively.

3. How can I access the elements of an array returned by a function?

To access the elements of an array returned by a function, you can use the index notation to specify which element you want to access. For example, myFunction()[0] will access the first element of the array returned by the myFunction() function.

4. Can I pass arguments into a function that returns an array?

Yes, you can pass arguments into a function that returns an array by adding parameters to the function declaration and using those parameters to manipulate the array before returning it. For example, function myFunction(param1, param2) { return [param1, param2]; } will return an array with the values of param1 and param2.

5. How do I handle errors when returning an array from a function?

To handle errors when returning an array from a function, you can use the try...catch statement to catch any errors that may occur during the execution of the function. Additionally, you can use throw to manually throw an error if needed.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
Replies
3
Views
858
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
1
Views
827
  • Programming and Computer Science
Replies
6
Views
887
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
8
Views
3K
Back
Top