Methods : Parameters & Arguments (Part 2)

In the last tutorial we learned about Methods Declaration and Calling them.

In this tutorial , we will learn about passing an argument and parameter into methods and the difference between them.



Method Parameters

Information can be passed to methods as parameter. Parameters act as variables inside the method.

Parameters are specified after the Method name, inside the parentheses. You can add as many parameters as you want, just separate them with a comma.

The following example has a method getName() that takes a String called name as parameter. When the method is called, we pass along a String, which is used inside the method to print the name:

 public class MyClass {  
      static void getName(String name) {  
           System.out.println("Heyy I am " + name + ".");  
      }  
      public static void main(String args[]) {  
           getName("Bob");  
           getName("Harry");  
           getName("John");  
      }  
 }  

Output:-

 Heyy I am Bob.  
 Heyy I am Harry.  
 Heyy I am John.  

When a parameter is passed to the method, it is called an argument. So, from the example above: name is a parameter, while Bob Harry and John are arguments.

In other words , parameters are used when declaring a method and arguments are used when calling a method. 


➤Using Multiple Parameters

You can use multiple parameters while calling a method.

Here I am using a method named getInfo() with parameters int age and String name.
 public class MyClass {  
      static void getInfo(String name, int age) {  
           System.out.println("Hi I am " + name + " and I am " + age+ " years old.");  
      }  
      public static void main(String args[]) {  
           getInfo("Bob", 18);  
           getInfo("Harry", 19);  
           getInfo("John", 24);  
      }  
 }  
Output:-
 Hi I am Bob and I am 18 years old.  
 Hi I am Harry and I am 19 years old.  
 Hi I am John and I am 24 years old.  
🔔 Note: When you are working with multiple parameters, the method call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.
 

➤ Return Values

In the above examples we used void keyword , it means while calling the method it does not return anything i.e returns void.

But if we want a string or an int or any dataType to be returned we call a method , that is where return keyword comes into role. 
 public class MyClass {  
      static int addOperation(int x,int y) {  
      return x+y;  
      }  
      public static void main(String args[]) {  
           System.out.println(addOperation(5,6));  
      }  
 }  
Output:-
 11  
You can also store the result in a variable. This is a better practice and also makes the code easier to read and debug.
 public class MyClass {  
      static int addOperation(int x, int y) {  
           return x + y;  
      }  
      public static void main(String args[]) {  
           int z = addOperation(10, 5);  
           System.out.println(z);  
      }  
 }  
Output:- 
 15


➤ Method using loops

You can use any loop in a method . Here I am going to show you how to use if-else statement.

Here I have declared a method called checkAge() which takes int age as a parameter. Output prompt is generated as "Enter your age: " and user inputs an age , the program calls the checkAge() method which returns age and uses if-else loop to check whether age is above 18.

Here I have used the concepts of JAVA Loops and Scanner , if you are not through with those , check those out first ! 
 import java.util.Scanner;  
 public class MyClass {  
      static int checkAge(int age) {  
           return age;  
      }  
      public static void main(String args[]) {  
           Scanner scan = new Scanner(System.in);  
           System.out.println("Enter your age: ");  
           int age = scan.nextInt();  
           checkAge(age);  
           if (age < 18) {  
                System.out.println("You are not an adult !");  
           } else {  
                System.out.println("You are an adult !");  
           }  
           scan.close();  
      }  
 }  
Output:-
 Enter your age:   
 25  
 You are an adult !  




< Previous Next >