| by Arround The Web | No comments

Return Array From Function C++

Arrays store the data of the same type in a consecutive memory location. Functions break the bigger problems into smaller chunks to make it easier for programmers to code. Another advantage of using the function is that it makes the code look presentable and clean. Sometimes, we need a function that returns an array so that we can utilize that array in other functions. Then, comes the concept of the array return function in C++. It is the function that returns the array to another function. In this Linux Hint tutorial we will show you with examples how to return arrays from functions that you write in the C++ language.

Methods:

To return a complete array on a single function call is not possible. We do this with different techniques. There are three methodologies in C++ to return an array from a function. We will explain all of them one after another with codes.

Return the array from a function using:

  • Pointers
  • Structure
  • std::array

Pointers

Pointers in any programming language are used to hold the memory address of another memory location in it. Pointers are also used to pass one function to another function. With this, we can pass an array data to the other functions.

In pointers, there are also two approaches to return the array from a function:

  • Dynamically allocated array
  • Statically allocated array

Structure

The structures are utilized to store the different data types in it. Inside the structures, we can store the array and make an instance of that structure. This way, we can return the array. We can make more than one instance of structure to access the array that is stored in the structure.

Std::Array

This is another function that is used to return the array from the function in C++. It also provides two methods – one is the size() and the other is empty(). The std::array, we can say, is a template of structures. But the two methods it provides are the plus point of using this function to get the array from any function. This function is rarely used; we often use pointers and structure.

Example 1:

Now, to explain how we can use the pointers to return the dynamic array from a function, look at the following code:

#include <iostream>
using namespace std;
char* character_func()
{
    char* array_0 = new char[3];
    array_0[0]='a';
    array_0[1]='b';
    array_0[2]='c';
    return array_0;
}
int main()
{
    char* p = character_func();
    cout<< "The character array is  ";
    cout <<"\t"<< p[0] << " " << p[1]<< " " << p[2];
    return 0;
}

 
After declaring the library, define a character_func()function to store the array in it. The function is a return type function. The return type is “char” which returns the character array. Char* tells that it is a pointer to store the character type data. Inside this function, declare an array of the pointer to store the character array. The size of the array is 3. Then, the array is initialized by the index number and returns the array variable. We store the character array in “array_0”. In the main() method, create a pointer “p” of the character type and store the character_func() in it. This means, with the help of this pointer “p”, we can access the elements in character_func(). Then, show a message on the terminal with the “cout<<” statement. Similarly, print the array components by accessing them with the pointer “p” from the return type function. Cout<< “\t” leaves a tab space, <<p[0] accesses the array_0[0] from the function, and so on.

Example 2:

Here, we utilize the pointers to return the static array from a function.

#include <iostream>
using namespace std;
float* floating_func()
{
    static float array_1[2];
    array_1[0] = 3.5;
    array_1[1] = 5.7;
    return array_1;
}
int main()
{
    float* pointer = floating_func();
    cout<<"\n\t The floating point static array is ";
    cout <<"\t"<< pointer[0] << "\t" << pointer[1]<<endl;
    return 0;
}

 
In this example, let’s take a return type pointer function of float type that returns the floating point array. Within the floating_func()function, initialize a floating point array – “array_1” – of size 2. Here, we use the keyword “static” to set the variable as a local variable that can be accessed outside the function. Now, declare the array and returns the array variable. In the main() function, define a floating point pointer and assign a floating_func() function to it. Then, represent a message on the screen. With the help of a pointer, we can now access all the elements of the return type function. Show the array elements by calling a pointer. The pointer goes inside the function and gets the array that we set as the return. Employ the “return 0” statement to terminate the code.

Example 3:

In this last instance, we return an array from a function using structures.

#include <iostream>
using namespace std;
struct arr {
    int a[5];
};
struct arr arr_func()
{
    struct arr val;
    val.a[0] = 5;
    val.a[1] = 67;
    val.a[2] = 98;
    val.a[3] = 77;
    val.a[4] = 55;
    return val;
}
int main()
{
    struct arr val = arr_func();
    cout<<"\n\tThe array is \n";
    for(int i=0; i<5; i++)
    {
        cout <<"\t"<< val.a[i]<<endl;
    }
    return 0;
}

 
Define the “arr” structure using the “struct” keyword. In this struct, initialize an array of size 5. Then, we declare the different variables and access them with the name of the structure to use them in the functions. Now, create a structure-function arr_func() to initialize the array. To do so, define a “struct” to call the “val” variable. The “val” variable accesses the location of the “struct” array and store the value in it. All the array items are now stored in “val”. In the main() method, create the instance of “struct” and assign the arr_func() function to it. Print “The array is” text on the console by calling the “cout<<”. To print the array, we utilize the “for” loop. Set the “for” to iterate 5 times. Insert the statement inside the curly braces so that we get the items of the array. Outside the loop, pass the “return 0” statement.

Conclusion

This article illustrates on how to return an array from a function in C++. The array can be returned from the function with three different approaches. Each methodology is explained in detail, following the coding example. Everything is addressed from the very beginning including the usage of pointers and their types, the use of structures, and the std::array function. C++ does not have built-in methods like the other languages provide, so we have to do everything on our own. C++ is best for beginners to understand the basics of coding.

Share Button

Source: linuxhint.com

Leave a Reply