Important Terms:
- Arguments: An argument is a value that is passed to the function when the function is called.
- Parameters: These are variables used to assign values/arguments.
Note: While defining the function it is mandatory to provide data types for variables/parameters but while calling the function, there is no need to provide data types for arguments.
Now let’s look at how to pass 2d arrays as arguments to the function:
- Whenever a 2D array is passed to the function the function will receive the passed array as it is without any change in values.
- The 2D array which is passed while calling the function will be received by the called function without any change in values of the array.
Let’s look at an example with Code of how to pass a 2D array?
Java Code
public class TUF {
public static void main(String[] args) {
int arr_2d[][] = {{1,2,3},{4,5,6},{7,8,9}};
//passing the arr_2d
passArray(arr_2d);
}
public static void passArray(int[][] arr) {
/*Here even we change the name of the array from 'arr_2d' to 'arr',
'arr' will receive the exact values of 'arr_2d' without any change
*/
//printing the values
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
Output:
1 2 3
4 5 6
7 8 9
- We can even return the 2D array from the function
Note: While returning the 2d Array from the function, the return type of the function should be “data-type[ ][ ]”
Let’s see how to do that with Code:
Java Code
public class TUF {
public static void main(String[] args) {
//Calling getArr function and storing in 2D Array
int arr_2d[][] = getArr();
//printing returned 2D array
printArr(arr_2d);
}
//function which will return 2D array
private static int[][] getArr() {
int arr[][] = {{1,2,3},{4,5,6},{7,8,9}};
//returning the 2d array from this function
return arr;
}
private static void printArr(int[][] arr_2d) {
for (int i = 0; i < arr_2d.length; i++) {
for (int j = 0; j < arr_2d[0].length; j++) {
System.out.print(arr_2d[i][j] + " ");
}
System.out.println();
}
}
}
Output:
1 2 3
4 5 6
7 8 9
- When a 2D array is passed to the function all the changes made in the function of the 2D array will reflect the original array.
Java Code
public class TUF {
public static void main(String[] args) {
int arr_2d[][] = {{1,2,3},{4,5,6},{7,8,9}};
//printing arr_2d values before passing to "changeArr" function
System.out.println("Before passing array as argument to the function");
printArr(arr_2d);
//passing the arr_2d
System.out.println("After passing array as argument to the function");
int getArr[][] = changeArr(arr_2d);
//Printing arr_2d values after passing to "changeArr" function and changing
//first row values
printArr(arr_2d);
}
public static int[][] changeArr(int[][] arr) {
//changing the values of first row
int row = 0;
int random_num = 34;
for (int i = 0; i < arr.length; i++) {
arr[row][i] = random_num;
random_num += 2;
}
return arr;
}
private static void printArr(int[][] arr_2d) {
for (int i = 0; i < arr_2d.length; i++) {
for (int j = 0; j < arr_2d[0].length; j++) {
System.out.print(arr_2d[i][j] + " ");
}
System.out.println();
}
}
}
Output:
Before passing array as argument to the function
1 2 3
4 5 6
7 8 9
After passing array as argument to the function
34 36 38
4 5 6
7 8 9
Special thanks to Sai bargav Nellepalli for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article