DEV Community

PRIYA K
PRIYA K

Posted on

Parameters,Arguments,Return Type

Parameters

  • Parameters are the variables declared in the method definition that receives the values.
  • Information passed to method as parameter.
  • Parameter are specified after the method name,inside the parentheses.

Example

public class Main {
  static void myMethod(String fname) {
    System.out.println(fname + " Refsnes");
  }

  public static void main(String[] args) {
    myMethod("Liam");
    myMethod("Jenny");
    myMethod("Anja");
  }
}
// Liam Refsnes
// Jenny Refsnes
// Anja Refsnes
Enter fullscreen mode Exit fullscreen mode

Method Parameters
Add as many parameters,and separate them with a commas.

public class Example {
    static void add(int a, int b) {   // a and b are parameters
        System.out.println(a + b);
    }

    public static void main(String[] args) {
        add(10, 20);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
int a ,int b => are parameters

Arguments
Arguments are the actual values passed to the method when calling it.

Example

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

    public static void main(String[] args) {
        add(10, 20);   // 10 and 20 are arguments
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation
10 and 20 are arguments

Return Type
• Return type defines what value a method returns
• void → no return value
• Other types (int, String, double, boolean) must use return statement
• Returned value goes back to the calling method
• Execution always starts from main()
• Without return type you cannot send data back

  • A return type in Java is the data type of the value that a method sends back to the calling method after execution.
  • When a method finishes its work, it can return a value using the return keyword.

Syntax

returnType methodName(parameters) {
    // code
    return value;
}
Enter fullscreen mode Exit fullscreen mode

Explanation

returnType → int, double, String, boolean, etc.
methodName() → name of the method
parameters → input values
return value; → sends the result back to the caller

Flow of Execution
Example:

class Demo {

    int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {

        Demo obj = new Demo();

        int result = obj.add(5, 3);

        System.out.println(result);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step-by-Step Execution
Program starts from main()
Object is created
Demo obj = new Demo();
Method call happens
obj.add(5, 3)
Control goes to add() method
Calculation happens
5 + 3 = 8
return 8 sends value back
Value stored in result
Printed on screen

Output
8

Why Return Type is Used ?
- To Send Result Back

class Demo {

    static int add() {
        return 5 + 3;
    }

    public static void main(String args[]) {

        int result = add();

        System.out.println(result);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

8

Here:

add() returns 8
main() receives it
Result is printed

1.Int Return Type
Returns an integer value.

public class Demo {
    static int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        System.out.println(add(5, 3));
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
8

2.Double Return Type
Returns a decimal number(Double Precision)

Example

public class Demo {
    static double divide(double a, double b) {
        return a / b;
    }

    public static void main(String[] args) {
        System.out.println(divide(10.5, 2.0));
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
5.25

3.Float Return Type
Returns a floating point number

Example

public class Demo {
    static float multiply(float a, float b) {
        return a * b;
    }

    public static void main(String[] args) {
        System.out.println(multiply(2.5f, 3.0f));
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
7.5

4.Char Return Type
Returns a single character

Example

public class Demo {
    static char getLetter() {
        return 'A';
    }

    public static void main(String[] args) {
        System.out.println(getLetter());
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
A

5.Boolean Return Type
Returns true or false

Example

public class Demo {
    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    public static void main(String[] args) {
        System.out.println(isEven(6));
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
true

6.String Return Type
Returns a string(text)

Example

public class Demo {
    static String greet() {
        return "Hello Priya";
    }

    public static void main(String[] args) {
        System.out.println(greet());
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
Hello Priya

7.Void Return Type(No return type)
Method does not return any value

Example

public class Demo {
    static void message() {
        System.out.println("Welcome to Java");
    }

    public static void main(String[] args) {
        message();
    }
}
Enter fullscreen mode Exit fullscreen mode

Example
Welcome to Java

Types Of Methods

1. Method with No Parameter and No Return Value
No input,does not return anything

class Demo {
    void greet() {
        System.out.println("Hello Priya");
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        d.greet();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
Hello Priya

Explanation
No Parameter inside ()
No Return Statement
Uses Void

2.Method with Parameter and No Return Value
takes input,does not return anything

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

    public static void main(String[] args) {
        Demo d = new Demo();
        d.add(5, 3);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
8

Explanation
Takes parameter a and b
Prints result directly
Uses Void

3.Method with Parameter and Return Value
Takes input and return a value

class Demo {
    int add(int a, int b) {
        return a + b;
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        int result = d.add(5, 3);
        System.out.println(result);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
8

Explanation
Takes parameter
Returns result using return

4.Method with No Parameter but Return Value
Takes no input , return value

class Demo {
    int number() {
        return 10;
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        int value = d.number();
        System.out.println(value);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output
10

Top comments (0)