Passing an Array to a Function C++

An array is a group of elements of the same data type. Many functions are performed on arrays either in the main program or outside it, in the functions. In C++, in the case of functions, we need to pass them. This is done via parameters as arguments. These arguments can be of different ways, either sizeable arrays or through the pointer array. In this tutorial, we will cover some major aspects of array transfer using different parameters of the functions.

Syntax

[Return type] [name of function] (data type arrayname[array size])

{

function body

}

Example 1

Consider an example in which we have to print the marks of students in the C++ program. This print will be taken in a separate function rather than in the main program. In contrast, we will take inputs in the main program and will transfer these values to the function as a parameter. Consider the function. In its parameter, there is an array datatype variable that will accept the values of an array. The full array is declared here. The marks will be displayed with the help of for loop. As in arrays, we need loops to get print from them.

Moving towards the main function, we declare an array with its size and values in it. As we have to call the function. So the method is that we write the function name with the name of the array in the parameter as an argument. We do not have defined the size of the array.

Display (marks);

The argument in the parameter implies the memory address of the array. In the parameter of the header of the function, int m[7] is converted into int *m. This includes the same address as the original array. When we use the m[5] in the body of the function, we are going to manipulate the original array.

Void display ( int m[7] )

In Linux operating system, getting output through a terminal requires some prerequisites to be installed. It needs a compiler to compile and then execute the code in the command prompt terminal. G++ is used in C++ for the compilation.

$ g++ -o code3 code3.c

$ ./code3

Where –o is used to store the output from the source file to the output file.

From the output, you can observe that all the numbers initiated in the array in the main function are passed and displayed through the display function.

Example 2

Another example regarding array passing through parameter is passing a multidimensional array to the function. A two-dimensional array (2d) is used here. In the main function, we need to initialize an array.

Int array[row][column]

2d array initialization includes row and column. And their order is to be retained throughout the program. 2d array is initialized with two numbers in the brackets. As we have described 2 columns in the initialization.

Display (num);

We will only use the array name in the parameter as an argument.

Now we will view the functioning of the display function. As the function is started, it takes an array variable to accept an array passed by the function call through the main program.

Void display( int n[][2] )

It is mandatory to mention the number of columns. In comparison, it is not essential in the case of rows. That’s why we have left the row brackets empty here as we use for loop to display the results. But in the case of a 2-dimensional array, we use a nested for loop. It contains two for statements with 2 variables in them.

We can view the output by utilizing the same compiler. You can see the results that each value is displayed separately with the row and the column number.

Example 3

This example is a bit dissimilar from the prior ones. In this example, we mention array size in the parameter of the function call. And in the function declaration, a variable is also introduced to accept the size of an array.

Starting from the main program, an array is initialized with the values.

Avg = getAverage(balance, 5);               

The result will be stored in the avg variable. Instead of passing only the array name, the array size is also added to the parameter.

The parameter also contains the array type variable and an integer data type to receive the array size. The type of the main program is int because it will receive an integer value from the function. Otherwise, it is void in other cases.

Now we will see the output. This value is visible through the image is obtained from the function.

Example 4

This example relates to determining the maximum number in the array, in fact, from two arrays. Here we initialize two arrays In the main program. Both arrays are separately passed to the function in separate function calls

printMax(arr1);

printMax(arr2);

where printMax is the name of the function and arr is the array. The result will not return from the function and is displayed there. For loop will calculate the maximum number in both arrays. If-statement is used inside the for loop. The header of the function is:

void printMax(int arr[5])

As both arrays contain different values, both the results will be different.

Example 5

This example is the summary of all types of arrays passing through the parameters. These might be sized, unsized or pointer arrays. We will consider them one by one.

In the main program, each function is declared first. You can point out the difference in their declaration.

Int sum1(int tmp[5]);

Int sum2(int tmp[]);

Int sum3(int * tmp);

These three arrays depict that arrays can be pass having these parameters in the function.

After function initialization, we have the main program in which the array is declared. Unlike the previous example, one array is initialized instead of two, but it is passed in three different ways. Now we will see the function calls made here.

Total = sum1(sales);

Total =sum2(sales);

Total = sume3(sales);

The output is displayed in the main function, so a variable is declared to accept the value returned by the function. From all three function calls, you can see that here the parameters are the same. Each array contains only the name of an array. But the parameters of the function that accepts the array are different.

The inner body of all three functions is the same, as the sum of all the numbers is calculated from the usage of For loop. The methodology and the array’s values are the same; only there is discrimination between the parameters of the functions. Hence, it is proved that we can either use different methods to accept an array or perform the same functionality, and the answer is the same. We can confirm it by checking the output. Using the same compilation procedure, we will get the output shown in the below-appended image.

You can see that the answer is the same for all the three functions used.

Conclusion

In this article, the user will come to know about the passing method of an array in parameters. Arrays can be handle in many cases when it comes to returning the value or passing it in arguments.



from Linux Hint https://ift.tt/3eVIWsb

Post a Comment

0 Comments