How Pass array to function.
In : BSc IT Subject : Structure Programming Methodology - JavaIn Java, you can pass an array to a method (function) just like you pass any other variable. You don't need to pass each element one by one — just pass the array name (which is a reference to the array), and the method can access or modify its elements.
Example ;
public class ArrayToFunction {
// Method that takes an array as parameter
public static void printArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
// Passing the array to the method
printArray(numbers);
}
}