Types of Functions in Java


☞There are two types of functions in Java.

1. Pure Functions
2. Impure Functions

Pure Functions

☞A function which returns a value to its caller module, is called a pure function.

☞It is also called accesser method, as it does not change the states of an object.

Example

class Addition{
    
    int sum(int a, int b){
        int c=a+b;
        return c;   //returning a value to a calling function
    }
    
    public static void main(String args[]){
        Addition obj=new Addition();
        int result=obj.sum(5,6);    //calling a function
        System.out.println(result);
    }

}

Output

11

Impure Functions or Mutator

☞A function which may not return a value to its caller module, is called impure function.

☞It basically changes the state of an object during function call.

Example

class Addition{
    
    void sum(int a, int b){
        System.out.println(a+b);
    }
    
    public static void main(String args[]){
        Addition obj=new Addition();
        obj.sum(5,6);       //function calling
    }
    
}

Output

11