Java Program to Illustrate a Method with 2 Parameters and without Return Type


A method without a return type is termed a void method since it returns nothing. This method can accept multiple parameters. In this tutorial, we will implement Java programs illustrating a method with two parameters and without return type.

First of all, we are going to get acquainted with the syntax, examples, and, finally implementation.

Syntax

public static void method_name (int parameter1, int parameter2)

Here,

public − It is the access specifier specifying who can access this method.

static − It is mandatory to make the method static to avoid errors.

void − it denotes that this method returns nothing.

method_name − It is the name or tag of the method. This method can be invoked in the latter part of the program using the method name only.

Parameter1, parameter2 − List of parameters. It can be of any valid type depending on the requirement.

public static void add (int a1, int a2) {
   // java code to add two integers
};

Algorithm

  • Start the program by declaring the class.

  • Define the main method. Within the main method, assign values to the variables and call the method defined later in the program.

  • Define the method as per the need for e.g.- diff, product, and assign two parameters to it.

  • Define the code to be executed inside the defined method.

  • Since the method does not have any return type, the output must be displayed using System.out.println() or any other suitable output method.

Example

The following program is written to demonstrate how a method is written with 2 parameters and no return type.

A method named diff containing 2 parameters and no return type is created within a class named Difference. The diff method gets invoked in the main method and the value whose difference is to be computed is passed to the method.

// Java Program to demonstrate a method
// having 2 Parameters and no Return value
import java.util.*;
public class Difference {
   public static void main(String args[]){
      int n1 = 100;
      int n2 = 20;
      // Calling the diff method 
      diff(n1, n2);
   }
   public static void diff(int a, int b){
      int diff = a - b;
      // Displaying the difference between two numbers
      System.out.print("Difference of two numbers " +a+ " and " +b+ " is: " + diff);
   }
}

Output

Difference of two numbers 100 and 20 is: 80

Example

This is another program written to demonstrate how a method is written with 2 parameters and no return type.

A method named product containing 2 parameters and no return type is created within a class named Product. The product method gets invoked in the main method and the value whose product is to be computed is passed to the method.

// Java Program to demonstrate a method having
// 2 Parameters and no Return value
import java.util.*;
public class Product {
   public static void main(String args[]){
      double n1 = 100.0;
      double n2 = 20.0;
      // Calling the product method 
      product (n1, n2);
   }
   public static void product (double a, double b)	{
      double product = a * b;
      // Displaying the product of two nos.
      System.out.print("Difference of two numbers " +a+ " and " +b+ " is: " + product);
   }
}

Output

Difference of two numbers 100.0 and 20.0 is: 2000.0

Conclusion

This article throws light on how a method is defined in Java with two parameters and no return value. We began with the syntax and further saw an example and two java programs to have a clear picture of the topic.

Updated on: 11-Apr-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements