Return Array From Function C++

Arrays are specific containers that have values of the same data type. Functions in C++ perform operations on arrays, and these arrays are then returned to the main function. There are many approaches to describe this phenomenon. In this guide, some common methods are explained:

Use Pointers to Return the Static Array

When we use a normal array, there are chances of having some sort of abnormal results. To avoid this, we use a static array in our C++ code. Let us understand the example we have used. In this function, we have declared an array with 5 values the return type as mentioned here.

Int *function ()

As the value will be an integer type, so it is tagged as “int” in example below. As we have introduced the function as a pointer, the function will be a pointer type. After entering the values, an array is returned to the main program.

In the main program, we have made a function call. To accept the value that is returned from the function, we will use an integer variable. When the array is returned, we can access its values easily. The values will be printed manually.

Int* pointer = function ();

The purpose of the pointer is to locate the item that is present on index one of the array. In other words, it shows the address of the value in the array. Then, we use a function prototype that will return the pointer.

To see the output of the array returned through the function, we need to have access to the Ubuntu terminal in the case of Linux. This is due to the output being accessible through the Linux terminal. In Linux, we need a compiler to run C++ codes written in any text editor. This compilation is done through G++. The “-o” is used to store the output in a file. Here, we need the output file and the source code file. After compilation, we will execute the code:

$ g++ -o file1 file1.c
$ . /file1

From the output, we can see the array, which was initialized in the function, is displayed in the main function by using a static array, initialized manually and through the pointers.

Return Dynamically Allocated Array Using Pointers

Arrays can be returned by using dynamic allocation. Arrays can be dynamically allocated by using the word “new”. They will remain there until we delete them by ourselves. Static arrays are fixed in size, which means you have to provide size during initialization. Once the array is created, then it is hard to increase the size at run time or hereafter. But in the case of the dynamic array, we can add more items whenever we want because it expands as we enter values in it. So we don’t need to specify or identify any size.

Moving towards the example we have used here. We have used a dynamic array with the pointers as in the previous examples, where we have used pointers with the static arrays.

Int *function ()

After function declaration, arrays are declared dynamically:

Int *array = new int [100];

The term, “new”, is constantly used to create a dynamic array. We will perform operations on the array by entering values in it. After that, the array is returned to the main program:

Now, consider the main function. We have made the function call. As the array is returned, we add a pointer integer type variable to accept the value.

Int* pointer = function ();

The values that were stored in the array are printed manually. The output is obtained through the compilation and execution method.

Return Array Using the Structures

Structures are the containers like arrays. But array contains the value of the same data type at a time. And in the case of structures, they contain more than one data type value. We have taken a structure named “sample”. Here, the array declaration is inside the structures instead of functions. The return type is the name of the structure. The structure variable is returned to the main program. The structure uses the word “struct” for declaration.

Struct sample
{
Int arr[100];
};

After the structure declaration, we have used a function in which an object of structure is created. This object will be used to access the structure. This function will return the object of structure to the main function so that we can print the array through this object. A variable will get the values in the variable. This value is the integer number up to which we will enter values in the array. As in this example, we have selected 6 as the number. So, the numbers will be entered up to 6 in the array.

Struct sample func (int n)

Now, moving towards the main program, we have created an object to access the array through this:

Struct sample x;

After object initialization, a value is added to the variable up to which we want the numbers to be entered in the array. In a function call, we will pass the value in the parameter:

X = func (n);

We will have the display by using the for loop. The values are displayed through the object declared at the start of the main program:

The output indicates that 6 values are shown in the result as we have entered 6 numbers in the program.

Return Array Using Std

C++ uses many methods to return an array from the function. One of them is through std::array. It is a template of structure. This feature also provides two more functions that are size () and empty (). An array name is returned that indicates that the whole array is returned to the main program. Here, we will add a header file “array”. In addition to the library, it contains all the functions of the array.

#include <array>

array<int,10> function()

Since we can return the whole array with the name of it, so in the declaration of a function, we will use the array as a return type. Data is entered in the array. After that, the array will be returned to the main program. Moving towards the main function, an array variable will accept the array when the function is called.

arr = function();

Again, for loop will be used for displaying array values. We observe the output from the image displayed below. As we have used 10 sizes, 0 numbers will be entered. Hence, these are displayed:

Return Array Through Vector Container

This approach is a dynamically allocated array. As in this case, there is no need to specify array size. We don’t need any size parameter here. Using this example, we need to add a “vector” header in the library that contains the functionalities of the vector.

Moving toward the function, where the return type is also an int vector and also contain a vector pointer as an argument in the parameter. An array with the name “temp” is introduced here:

Vector <int> MultiplyArrayByTwo(const vector<int> *arr)

The function will multiply the elements of the array by two through using the tmp.push_back () function. Then, return the tmp. An auto-type variable will accept the values of the array from the function. The array contains the items in it.

The output shows the working of the vector container.

Conclusion

In the aforementioned article, we have described the five most commonly used methods to explain the functionality of returning an array from the function.



from Linux Hint https://ift.tt/2TEZoG3

Post a Comment

0 Comments