Function in java



Function is a block of code. It will Execute , when you are call you. The main reason for using function/method is reduce the code and Easy to understand.








Example-1
public class Main
{
      public static void main(String[] args) 
      {
            Main obj = new Main();

            obj.show();
      }

      public void show()
      {
            System.out.print("mycrazycoding");
      }
}



Output



mycrazycoding









Example-2
public class Main
{
      public static void main(String[] args) 
      {
            Main obj = new Main();

            obj.show();
            obj.show();
            obj.show();

      }

      public void show()
      {
            System.out.println("mycrazycoding");
      }
}



Output



mycrazycoding
mycrazycoding
mycrazycoding









Example-3
public class Main
{
      public static void main(String[] args) 
      {
            Main obj = new Main();

            obj.add(5,6);
      }

      public void add(int a,int b)
      {
            System.out.println(a+b);
      }
}



Output




11









Other related topics



1.Types of Function 2.Variable Arguments (Varargs) 3.Recursion