Lesson 7 :- Types of Methods.

In the previous lesson, I told about method calling.

Now I'm going to distribe types of methods. There are three types of methods. They are,
                                                 1.Dumb method
                                                 2.Clever method
                                                 3.Smart method

First of all I would like to tell below exercise to perform.

    int a = 5
    int b = 10

* find (a+b)

Dumb method

    public class Cal1{
             public static void main(String[] args){

                   addition();

             }
       
             public static void addition(){
               
                   int a=5;
                   int b=10;
                   System.out.println("a+b = "+(a+b));

            }
   }




































We type the operation under a method call "addition". Then we call it in the main method.

This method is called as "Dumb method".


Clever method

     public class Cal2{
             public static void main(String[] args){

                   addition(5,10);

             }
       
             public static void addition(int a, int b){
           
                   System.out.println("a+b = "+(a+b));

            }
   }



































We are calling parameters in this method. int a & int b are parameters. Firstly we create a method. Then we write the operation in that method.

This method is called as "Clever method".


Smart method

     public class Cal3{
             public static void main(String[] args){

                   int sum = addition(5,10);
                   System.out.println("a+b = "+sum);

             }
       
             public static int addition(int a, int b){
               
                  return (a+b);

            }
   }


































Special thing of this method is using a return type. In the previous lessons, we used void return type only. But now we use "int" return type. It means, we are passing int value. We need to change the return type to suit our input values.

This method is called as "Smart method".




Try to understand these three methods & select one method to continue the lessons.

Comments

Popular posts from this blog

Lesson 1

Lesson 6 :- Calling a method

Lesson 8 :- Creating an object.