How do you return an array from a method?

Normal arrays can't be returned from a function because their lifetime ends when the function returns, the behavior for accessing one of these local arrays outside the scope of the function is undefined.

In your particular case this is possible because your array has static storage duration.

In C a function can only return one element, so to return an array you must return a pointer which can contain the address of the first element of that array. And that's what you're doing in your code. You can access both values by correctly indexing the returned pointer i.e. values[0] and values[1].

Unfortunately this is not without its issues, the size of the array is not known by the caller and you can't safely index it because you don't know its bounds.

There are ways solve this which are not all that complicated once you get used to them. Most notably defining a global size for the array1, using a structure containing the size of the array and a pointer to the array itself2, or passing pointers to the size and/or the array as arguments of the function3.

1. Using a global variable that stores its size:

#define SIZE 2

int *test()
{
    static int states[SIZE] = {4, 7};
    return states; //returns a pointer to the first element of the array
}

int main()
{
    int* values = test(); // values is now pointing to the array
    
    for(size_t i = 0; i < SIZE; i++){
        printf("%d ", values[i]); //indexing is similar to a local array
    }
}

2. Using a struct to store both the size and a pointer to the array:

typedef struct{ //structure to hold the data
    int *array;
    size_t size;
} array_struct;

array_struct test()
{  
    static int states[2] = {4, 7};
    array_struct array = {.array = states, .size = 2}; //assing pointer and size
    return array; //return the structure
}

int main()
{
    array_struct values = test(); //assing the structure to a local
    
    for(size_t i = 0; i < values.size; i++){ //use the size passed
        printf("%d ", values.array[i]);
    }
}

Output:

4 7 

Option 3 is laid out in Bathsheba's answer.

As we know that arrays are very important for a programming language as they group the values of same data type in one variable so in Java array also play a vital role. When we create functions or methods we usually pass variables as arguments. But what if we want to return a large amount of data having same data type at once from a function or method?

We can do that by returning an array where we want to use numerous values of the same data type without occupying the large memory space.

In Java, we can return an array from a function. The following practical example, will showcase how to return an array practically in Java.

Code:

public class arry {
    public static int[] rtnarray() {
        int[] ary = {0,2,4,6,8,10,12,14,16,18,20};
        return ary;
    }
    public static void main(String[] args) {
        int[] get = rtnarray();
        int q = 0;
        while(q<get.length)
        {
            System.out.println("The value at index-number " + q + " is: " +get[q]);
            q++;
        }
}
}

In this code, we create a static function which will return an integer type array. Then in the main function, we create an integer type array variable and initialize it with the function that returns an integer array. Lastly, we use a while loop to display the elements of the array.

Output:

How do you return an array from a method?

The output clearly shows that we can return an array with the help of a method and display the required result.

Here you go! You have learned to return an array in Java.

Conclusion

In java, an array can be returned with the help of a method or a function. For this purpose, the method return type must be the type of the array, and the variable that stores the array also has the same data type as the array. In this article, we talked about we have gone through the prose in detail through which we can return an array in Java.

About the author

How do you return an array from a method?

I am a computer science graduate with a passion to learn technical knowledge and share it
with the world. I love to work on top state-of-the-art computing languages. My aim is to best
serve the community with my work.

How can we return array in Java?

Keypoint 1: Method returning the array must have the return type as an array of the same data type as that of the array being returned. The return type may be the usual Integer, Double, Character, String, or user-defined class objects as well.

Can you return arrays from functions in C?

C programming does not allow to return an entire array as an argument to a function. However, you can return a pointer to an array by specifying the array's name without an index.

When an array is passed to a method What does the method receive?

When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference). Therefore, any changes to this array in the method will affect the array.

Can you return multiple arrays from a method in Java?

You can return only one value in Java. If needed you can return multiple values using array or an object.