Array as Parameter C++

Functions in C++ can accept different types of arguments when they are called. In the same manner, an array can also be passed as a parameter to a function in C++. To figure out whether passing an array as a parameter to a function in C++ is the same as passing any other type of argument to a function or not, you will have to give a read to this article.

Passing Array as Parameter in C++ in Ubuntu 20.04:

Whenever you want to deal with multiple values within a function, you might need to store those values within an array. Then this array can be passed to that function as a parameter depending upon the needs of the function. There are different ways of passing an array as a parameter in C++. We will discuss all of these methods in detail in the following appended illustrations.

Example # 1: Passing an Array with Undefined Size

An array can be passed to a function in C++ in a way that its size is not defined. To understand this method of passing an array to a function in C++, you will have to take a look at the code shown below:

In this example code, we have defined a function named “printElements()”. This function accepts two different arguments, i.e., “int arr[]” which refers to the array of undefined size, and “int len” which corresponds to the size of that array. Then, within the body of this function, we just have a “for” loop that iterates to the size of this array. This loop is there to print all the elements of the array passed to this function as a parameter on the terminal.

Then, within our driver function, we have defined an integer variable “size” and assigned the value “5” to it. Then, we declared an array of size “size” and assigned five different integer values to it. After that, we have simply called the “printElements()” function with the name of the array and the “size” variable.

To compile this example program, we executed the following command:

$ g++ ArrayParam.cpp –o ArrayParam

To see if our array has been correctly passed as a parameter to a function of our program, we have to execute our code with the command shown below:

$ ./ArrayParam

The output of this program, i.e., all the elements of the array passed as a parameter to the “printElements()” function, is shown in the following image:

Example # 2: Passing an Array with Predefined Size as Parameter to a Function in C++:

An array can also be passed to a function in C++ in a way that its size is predefined in the function’s prototype. To understand this method of passing an array to a function in C++, you will have to take a look at the code shown below:

In this example code, we have defined a function named “printElements()”. This function accepts only a single argument, i.e., “int arr[5]” which refers to an integer array of size “5”. Then, within the body of this function, we just have a “for” loop that iterates till the size of this array, i.e., “5” in this case. This loop is there to print all the elements of the array passed to this function as a parameter on the terminal.

Then, within our driver function, we have simply declared an array of size “5” and assigned five different integer values to it. After that, we have just called the “printElements()” function with the name of our integer array.

The output of this program, i.e., all the elements of the array passed as a parameter to the “printElements()” function, is shown in the following image:

Example # 3: Passing an Array as Pointer to a Function in C++:

An array can be passed in yet another way to a function in C++ as a pointer. To understand this method of passing an array to a function in C++, you will have to take a look at the code shown below:

In this example code, we have defined a function named “printElements()”. This function accepts two different arguments, i.e., “int *arr” which refers to a pointer to that array, and “int len” which corresponds to the size of that array. Then, within the body of this function, we just have a “for” loop that iterates to the size of this array. This loop is there to print all the elements of the array passed to this function as a parameter on the terminal.

Then, within our driver function, we have defined an integer variable “size” and have assigned to it the value “5”. Then, we declared an array of size “size” and assigned five different integer values to it. After that, we have simply called the “printElements()” function with the name of the array and the “size” variable.

The output of this program, i.e., all the elements of the array passed as a parameter to the “printElements()” function, is shown in the following image:

You would have noticed that the output of all the three programs discussed above was exactly the same, which means that you have just learned three different ways of doing the same thing.

Example # 4: Passing a Multi-Dimensional Array to a Function in C++:

We can also pass a multi-dimensional array to a function in C++ very easily. To understand this method of passing an array to a function in C++, you will have to take a look at the code shown below:

In this example code, we have defined a function named “printElements()”. This function accepts only a single argument, i.e., “int arr[2][2]” which refers to a two-dimensional (2D) array of size “2 x 2”. Then, within the body of this function, we just have a nested “for” loop that iterates to the size of this array. This loop is there to print all the two-dimensional array elements passed to this function as a parameter on the terminal.

Then, within our driver function, we have simply declared a two-dimensional integer array of size “2 x 2” and have assigned to it two different pairs of integer values. After that, we have just called the “printElements()” function with the name of this two-dimensional array.

The output of this program, i.e., all the elements of the two-dimensional array passed as a parameter to the “printElements()” function is shown in the following image:

Conclusion:

This article was meant to draw your attention to the different methods of passing an array as a parameter to a function in C++ in Ubuntu 20.04. For doing so, we precisely discussed four different methods with you through which you can easily pass one-dimensional as well as multi-dimensional arrays to the functions in C++. Therefore, these examples will help you a lot whenever you intend to pass an array as a parameter to a function in C++ in Ubuntu 20.04.



from https://ift.tt/3mBY16m

Post a Comment

0 Comments