When we create a method, sometimes we want to give it some information to work with. Other times, we want the method to give us a result back after doing its job.
This is where method parameters and return values come in.
For example, imagine we create a method that adds two numbers. We need to give the method two numbers, and after calculating the result, we want it to give the answer back to us.
static int add(int a, int b) {
return a + b;
}
Here, a and b are parameters, and the method returns the result of a + b.
Method Parameters
A parameter is a variable that we define inside the method's parentheses. It allows the method to receive data when it is called.
For example:
static void greet(String name) {
System.out.println("Hello, " + name);
}
Here, name is a parameter.
We can pass a value to it when calling the method:
greet("John");
The value "John" is passed to the name parameter, so the output becomes:
Hello, John
We can call the same method with a different value:
greet("Jason");
Output:
Hello, Jason
This is useful because we don't need to create a separate method for every person. The same method can work with different values.
Multiple Parameters
A method can have more than one parameter.
For example:
static void introduce(String name, int age) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
Now we need to provide both values when calling the method:
introduce("John", 25);
Output:
Name: John
Age: 25
The order matters. The first value is assigned to the first parameter, and the second value is assigned to the second parameter.
introduce("John", 25);
Here, "John" goes into name and 25 goes into age.
What is a Return Value?
A method can also perform some operation and send a value back to the code that called it. This value is called the return value.
For example:
static int add(int a, int b) {
return a + b;
}
This method takes two integers and returns an integer.
We can call it like this:
int result = add(10, 20);
System.out.println(result);
Output:
30
The method calculates 10 + 20 and returns 30. That value is then stored in the result variable.
The return Keyword
The return keyword is used to send a value back from a method.
For example:
static int square(int number) {
return number * number;
}
If we call:
int result = square(5);
the method calculates:
5 × 5 = 25
and returns 25.
So result will contain 25.
A method can return different types of values depending on its return type.
For example:
static int getAge() {
return 25;
}
returns an int.
static double getPrice() {
return 99.99;
}
returns a double.
And:
static String getName() {
return "John";
}
returns a String.
void Methods
What if a method doesn't need to return anything?
That's where void is used.
static void greet(String name) {
System.out.println("Hello, " + name);
}
This method performs an action but doesn't send a value back.
That's why its return type is void.
Compare these two methods:
static void greet(String name) {
System.out.println("Hello, " + name);
}
and:
static int add(int a, int b) {
return a + b;
}
The first method doesn't return anything, while the second returns an int.
Parameters and Return Values Together
Parameters and return values are often used together.
For example:
static double calculateArea(double length, double width) {
return length * width;
}
Here, length and width are parameters, and the calculated area is the return value.
We can use it like this:
double area = calculateArea(10, 5);
System.out.println("Area: " + area);
Output:
Area: 50.0
The values 10 and 5 are passed into the method, the method performs the calculation, and the result is returned.
Parameters vs Arguments
There is a small terminology difference that is useful to know.
When we define the method:
static int add(int a, int b) {
return a + b;
}
a and b are called parameters.
When we call the method:
add(10, 20);
10 and 20 are called arguments.
So you can remember it this way:
Parameters → variables defined in the method
Arguments → actual values passed to the method
A Complete Example
Let's put everything together:
class Main {
static int calculateTotal(int price, int quantity) {
return price * quantity;
}
static void showTotal(int total) {
System.out.println("Total price: " + total);
}
public static void main(String[] args) {
int total = calculateTotal(100, 3);
showTotal(total);
}
}
Output:
Total price: 300
Here, calculateTotal() receives price and quantity as parameters and returns the calculated total. That returned value is stored in total, which is then passed to showTotal().
Once you understand parameters and return values, methods become much more powerful because they can receive information, process it, and send a result back.