Passing Array to Function In Java

Functions are used to break and divide a huge code into small chunks so that the code becomes more understandable and thus reduces the complexity of the code. Arrays are the homogenous data structures for reducing the code complexity, increasing efficiency, and reducing the execution time of the code. We make use of the array and think of both these time-saving parts of programming are implemented together. Thereby exists the concept of Passing Array to Function.

In this section, we will learn how we can pass an array to a user-defined function and will notice how it makes the code more optimistic as well as efficient.

Generally, the purpose of passing an array to a function is to transfer a large amount of data between methods. To pass an array to a function, just pass the array as function's parameter (as normal variables), and when we pass an array to a function as an argument, in actual the address of the array in the memory is passed, which is the reference. Thus, any changes in the array within the method will affect the actual array values.

We will implement some example codes through which we will get to know how to pass an array to a function.

Example 1:

Calculating the maximum and minimum values of a given array.

In order to get the maximum and minimum value in an array, we can perform the below-shown code:

Output:

Passing Array to Function In Java

Code Explanation:

  • In the above code, we created a class array within which we have created a max() function and min()
  • In the max () function, we have passed an array as the function parameter, and using for loop, we found the maximum value in the array by indexing i from 0 to the length of the array, and as soon it finds the maximum value, it gets stored in the max variable. The condition is that if any of the elements present in the array is greater than the max variable (i.e., max=0 initially), then only it will assign that maximum value to the array.
  • Next, in the min () function, we performed the same, but in this, we have found the minimum value present in the array.
  • Finally, in the main () method, we printed the obtained max and min values from the array.

Example 2:

Computing an array of Random Numbers

Random numbers are those numbers whose occurrence is random and cannot be predicted reasonably.

Below is the example code through which we can understand the passing of an array to a function and generate random numbers:

On executing the code, we got the below-shown output:

Passing Array to Function In Java

Note: In the case of random numbers, always the result will vary as the numbers are generated randomly.

Example 3:

Sorting numbers of an array

Below is an example code where we pass an array to a function and sort the elements of the given array:

When we executed the above code, we got the following result:

Passing Array to Function In Java

These are some of the possible example implementations through which it is understandable that how one can pass an array to a function in Java.


Next TopicJava Error