Tutorial #5 : Method



Method used for reusability. There is two types of methods :- 
1. Return Type 
2. Parameter 

Creating Method :- 


  • public static − modifier
  • int − return type
  • methodName − name of the method,
  • a, b − formal parameters.
  • int a, int b − list of parameters.
  • void :- Not returning any thing .

Syntax

public static int methodName(int a, int b) {
   // body
}

Example
public class ExampleMinNumber {
   
   public static void main(String[] args) {
      int a = 11;
      int b = 6;
      int c = minFunction(a, b);
      System.out.println("Minimum Value = " + c);
   }

   /** returns the minimum of two numbers */
   public static int minFunction(int n1, int n2) {
      int min;
      if (n1 > n2)
         min = n2;
      else
         min = n1;

      return min; 
   }
}

Comments