When a method returns an array reference you include with the return type in the method header?

next → ← prev

In this section, we are going to learn how to return an array in Java.

Remember:

  • A method can return a reference to an array.
  • The return type of a method must be declared as an array of the correct data type.

Example 1

In the following example, the method returns an array of integer type.

Output:

When a method returns an array reference you include with the return type in the method header?

Example 2

In the following example, the method returns an array of double type.

Output:

When a method returns an array reference you include with the return type in the method header?

Example 3

In the following example, method returns an array of object type.

Output:

When a method returns an array reference you include with the return type in the method header?

Next TopicJava Tutorial

← prev next →

Introduction

In this tutorial, we are going to understand how we can return an array from a function in C++.

Methods to Return an Array in a C++ Function

Typically, returning a whole array to a function call is not possible. We could only do it using pointers.

Moreover, declaring a function with a return type of a pointer and returning the address of a C type array in C++ doesn’t work for all cases. The compiler raises a warning for returning a local variable and even shows some abnormal behavior in the output.

Hence, returning an array from a function in C++ is not that easy. But we can accomplish that by following any of the below mentioned methods.

Let’s get right into it.

1. Using Pointers

As we mentioned earlier, returning a normal array from a function using pointers sometimes gives us unexpected results. But this behaviour and warnings can be avoided by declaring the array to be a static one.

Let us see how.

#include<iostream>
using namespace std;

int* demo() //return type- address of integer array
{
	static int a[5]; //array declared as static
	for(int i = 0; i<5; i++)
	{
		a[i] = i; //array initialisation
	}

	return a; //address of a returned
}

int main()
{
	int* ptr; //pointer to hold address
	int i;
	ptr = demo(); //address of a
	cout<<"Array is: ";
	for(i=0 ; i<5; i++)
		cout<<ptr[i]<<"\t"; //ptr[i] is equivalent to *(ptr+i)
		
	return 0;
}

Output:

Array is: 0     1       2       3       4

Here, we have declared the function demo() with a return type int *(pointer) and in its definition, we have returned a (serves as both array name and base address) to site of the function call in main().

As we can see from the above output, the array is successfully returned by the function.

2. Using a Structure in C++

We can also make a function return an array by declaring it inside a structure in C++. Let us see how.

#include <iostream>
using namespace std;

struct demo
{
	//array declared inside structure
	int arr[100];
};

struct demo func(int n) //return type is struct demo
{
	struct demo demo_mem; //demo structure member declared
	for(int i=0;i<n;i++)
	{
		//array initialisation
		demo_mem.arr[i] = i;
	}
	return demo_mem; //address of structure member returned
}

int main() 
{
	struct demo a;
	int n=5; //number of elements
	
	a=func(n); //address of arr
	
	cout<<"The Array is : ";
	for(int i=0;i<n;i++)
	{
		cout<<a.arr[i]<<"\t";
	}
	
	return 0;
}

Output:

Array is: 0     1       2       3       4

Here, note that we have declared the array arr inside the structure demo. And this time the function has a return type of the structure itself and return demo_mem (structure variable) instead of the array.

In this way using another structure variable a, we can access the array arr in the main() function.

3. Using std::array

For std::array in C++, returning the array name from a function actually translates into the the whole array being returned to the site of the function call.

#include <iostream>
#include<array>
using namespace std;

std::array<int,5> func() //function with return type std::array
{
    std::array<int,5> f_array; //array declared
    
	for(int i=0;i<5;i++)
	{
		//array initialisation
		f_array[i] = i;
	}

    return f_array; //array returned
}

int main() 
{
	std::array<int,5> arr; //array with length 5
	
	arr=func(); //function call
	
	cout<<"The Array is : ";
	for(int i=0;i<5;i++)
	{
		cout<<arr[i]<<"\t";
	}
	
	return 0;
}

Output:

Array is: 0     1       2       3       4

Hence it is clear from the output, that the array return by the function func() was successful.

Conclusion

So in this tutorial, we learned about the different methods by which we can return an array from a C++ function.

For any further questions, feel free to use the comments below.

References

  • C++ return array from function - StackOverflow Question,
  • Two Dimensional Array in C++ - Journal Dev Post.

When a method returns an array reference you include ____ with the return type in the method header A B C D?

When returning an array reference, square brackets are included with the return type in the method header.

How do you return an array from a method?

How to return an array in Java.
import java.util.Arrays;.
public class ReturnArrayExample1..
public static void main(String args[]).
int[] a=numbers(); //obtain the array..
for (int i = 0; i < a.length; i++) //for loop to print the array..
System.out.print( a[i]+ " ");.

When you pass an array element to a method the method receives?

for(x = 0; x < 4; ++x) num[x] = 100; Unicode value \u0000 is also known as null. When you pass an array element to a method, the method receives a copy of the value in the element.

How do you return an array element in Java?

get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned.