ITP

BSc IT - Structure Programming Methodology - Java

How Pass array to function.

In : BSc IT Subject : Structure Programming Methodology - Java

In 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);
    }
}

About us

A truly open platform where you may ask questions and get answers. We also provide comprehensive and easy-to-understand answers to question papers.  discover...

Site status

Flag Counter

Privacy Policy

Sitemap