Java Function Example

public class Function {
    
    public static void main(String args[])
    {
        int a=10, b=2;
        
        /*
         * Each of the following 4 statements, calls
         * a function and prints the value returned.
         */
        System.out.println(a + "+" + b + "=" + addition(a,b));
        System.out.println(a + "-" + b + "=" + substraction(a,b));
        System.out.println(a + "*" + b + "=" + multiplication(a,b));
        System.out.println(a + "/" + b + "=" + division(a,b));
    }
    
    //A function returns the sum of two integers.
    public static int addition(int a, int b)
    {
        return a+b;
    }
    
    //A function returns the difference of two integers.
    public static int substraction(int a, int b)
    {
        return a-b;
    }
    
    //A function returns the multiplication of two integers.
    public static int multiplication(int a, int b)
    {
        return a*b;
    }
    
    //A function returns the division of two integers.
    public static int division(int a, int b)
    {
        return a/b;
    }
}

Search within Codexpedia

Custom Search

Search the entire web

Custom Search