Passing an Array to a Function in Java Last Updated : 13 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Passing an array to a function is an easy-to-understand task in Java. In this article, we will check how to pass an array as a method parameter.Caller Function Vs Called FunctionLet function GFG() be called from another function GFGNews(). Here, GFGNews is called the “Caller Function” and GFG is called the “Called Function OR Callee Function”. The arguments/parameters which GFGNews passes to GFG are called "Actual Parameters" and the parameters in GFG are called "Formal Parameters". The array to pass can be a one-dimensional(1D) array or a multi-dimensional array such as a 2D or 3D array. Syntax to Pass an Array as a Parameter1. Caller Functioncalled_function_name(array_name);The code for the called function depends on the dimensions of the array. The number of square brackets in the function prototype is equal to the dimensions of the array, i.e. [n] for 1D arrays, [n][n] for 2D arrays, [n][n][n] for 3D arrays, and so on.2. Called FunctionOne Dimensional ArrayreturnType functionName(datatype[] arrayName) { //statements}Two Dimensional ArrayreturnType functionName(datatype[][] arrayName) { //statements}Here:returnType: the return type of the called functionfunctionName: name of the called functiondatatype: the datatype of the arrayarrayName: name of the arrayNote: We can also write the function in the similar way for 1 and 2 Dimensional mentioned below.returnType functionName (datatype arrayName[])returnType functionName (datatype arrayName[][])Example to Pass an Array to a Function Java // Java Program to Pass an Array to a Function import java.io.*; class GFG { void function1(int[] a) { System.out.println("The first element is: " + a[0]); } void function2(int[][] a) { System.out.println("The first element is: " + a[0][0]); } public static void main(String[] args) { GFG obj = new GFG(); int[] arr1 = { 1, 2, 3, 4, 5 }; int[][] arr2 = { { 10, 20, 30 }, { 40, 50, 60 }, { 70, 80, 90 } }; // passing the 1D array to function 1 obj.function1(arr1); // passing the 2D array to function 2 obj.function2(arr2); } } OutputThe first element is: 1 The first element is: 10 Comment More infoAdvertise with us Next Article Passing an Array to a Function in Java H himanshu20032002 Follow Improve Article Tags : Java Java-Arrays Practice Tags : Java Similar Reads How to Return an Array in Java? An array is a data structure that consists of a group of elements of the same data type such that each element of the array can be identified by a single array index or key. The elements of the array are stored in a way that the address of any of the elements can be calculated using the location of 5 min read Convert List to Array in Java The List interface provides a way to store the ordered collection. It is a child interface of Collection. It is an ordered collection of objects where duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. Now here we are give 5 min read How to Declare an Array in Java? In Java programming, arrays are one of the most essential data structures used to store multiple values of the same type in a single variable. Understanding how to declare an array in Java is very important. In this article, we will cover everything about array declaration, including the syntax, dif 3 min read Array to ArrayList Conversion in Java In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th 3 min read Array getInt() Method in Java The java.lang.reflect.Array.getInt() is an inbuilt method in Java and is used to return an element at the given index from the specified Array as a int. Syntax Array.getInt(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The object array whose index is to 3 min read Like