Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java Parameterized Methods

Till now we have been using methods that do not accept anything from caller method and do the fixed operation. But what if we want to pass some values to the called method from caller method? Does Java provide facility to achieve this type of condition? Yes! Java has provided us the parameterized methods to achieve this type of condition.

Parameterized method is the method that can accept values sent from the caller method.

Suppose we want to create two methods that can perform addition & subtraction on different values sent from caller method. Then we should create two parameterized methods that can accept values sent from caller method.

Note

The values that are sent from caller method are known as arguments and the variables that are used in method header to receive values are known as parameters.

Example of parameterized methods

 public void addition(int a, int b, int c)
{
    int result = a+b+c;
    System.out.println("Addition :: "+result);
}

Above addition() method can receive any three int values from caller method and operate on it.

 public void subtraction(double a, double b)
{
    double result = a-b;
    System.out.println("Subtraction :: "+result);
}

Above subtraction() method can receive any two double values from caller method and operate on it.

Let’s see how parameterized method works

public class Calculation 
{
     public void addition(int a, int b, int c) 
     {
         int result = a+b+c;
         System.out.println("Addition :: "+result);		
     }
	
     public void subtraction(double a, double b) 
     {
         double result = a-b;
         System.out.println("Subtraction :: "+result);		
     }
	
     public static void main(String args[])
     {
         Calculation c = new Calculation();
         c.addition(10,20,30);
         c.subtraction(50.54,15.11);	
     }
}

Output

Addition :: 60
Subtraction :: 35.43

For the better understanding of the above program look at the diagram shown below:

Java Programming Language Parameterized Method Working

Arguments can be of any type. Like constant, expression, variable or function call.

Note

To understand the program given below you need to understand topic Returning a value from method.

Example

public class Calculation2 
{
    public static int addition(int a, int b, int c) 
    {
        int result = a+b+c;
        return result;
    }
	
    public static double subtraction(double d, double e) 
    {
        double result = d-e;
        return result;
    }
	
    public static void main(String args[]) 
    {
        int x = 5;
        int y = 6;
        int add;
        double sub;
        
        add = addition(x,3,x*y);
        System.out.println("Addition = "+add);
        
        sub = subtraction(subtraction(15.5, 5.5), 5.5);
        System.out.println("Subtraction = "+sub);
    }
}

Output

Addition = 38
Subtraction = 4.5

Note

A method cannot be defined inside another method definition. That is, methods cannot be nested.

 

Leave a reply