// 14_Functions.java - פונקציות (methods) ב-Java

public class Functions {
    public static void main(String[] args) {
        // קריאה לפונקציות
        greet("נפתלי");

        int sum = add(5, 3);
        System.out.println("5 + 3 = " + sum);

        double area = circleArea(5.0);
        System.out.println("שטח עיגול עם רדיוס 5: " + area);

        // העמסה - אותה פונקציה עם פרמטרים שונים
        System.out.println("max int: " + max(10, 20));
        System.out.println("max double: " + max(3.5, 2.8));
        System.out.println("max 3 ints: " + max(5, 15, 10));

        // פרמטרים בכמות משתנה
        System.out.println("\nסכום של 3 מספרים: " + sumAll(1, 2, 3));
        System.out.println("סכום של 5 מספרים: " + sumAll(1, 2, 3, 4, 5));
        System.out.println("סכום ריק: " + sumAll());

        // שימוש בפונקציות עזר
        int[] arr = {1, 2, 3, 4, 5};
        System.out.println("\nסכום המערך: " + sumArray(arr));

        // פונקציה שמחזירה boolean
        System.out.println("7 ראשוני? " + isPrime(7));
        System.out.println("10 ראשוני? " + isPrime(10));

        // פונקציה שמשנה מערך
        int[] numbers = {1, 2, 3, 4, 5};
        doubleValues(numbers);
        System.out.print("אחרי הכפלה: ");
        for (int n : numbers) System.out.print(n + " ");
        System.out.println();
    }

    // פונקציה ללא החזרה
    public static void greet(String name) {
        System.out.println("שלום " + name);
    }

    // פונקציה שמחזירה ערך
    public static int add(int a, int b) {
        return a + b;
    }

    public static double circleArea(double radius) {
        return Math.PI * radius * radius;
    }

    // העמסה
    public static int max(int a, int b) {
        return (a > b) ? a : b;
    }

    public static double max(double a, double b) {
        return (a > b) ? a : b;
    }

    public static int max(int a, int b, int c) {
        return max(max(a, b), c);
    }

    // varargs - פרמטרים בכמות משתנה
    public static int sumAll(int... numbers) {
        int total = 0;
        for (int n : numbers) {
            total += n;
        }
        return total;
    }

    // מקבל מערך
    public static int sumArray(int[] arr) {
        int total = 0;
        for (int n : arr) {
            total += n;
        }
        return total;
    }

    // מחזיר boolean
    public static boolean isPrime(int n) {
        if (n < 2) return false;
        for (int i = 2; i * i <= n; i++) {
            if (n % i == 0) return false;
        }
        return true;
    }

    // משנה מערך (pass by reference של האובייקט)
    public static void doubleValues(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] *= 2;
        }
    }
}
