Method Parameters and Return Values in Java
A Simple Thought
Imagine asking your friend to calculate the total price of three apples. You wouldn’t want them to guess the number; you’d tell them the price of one apple and how many you bought.
In the same way, when you create a method in Java, you can send it information to work with. These pieces of information are called parameters.
And sometimes, the method can give something back when it’s done — this is called a return value.
In this lesson, you’ll learn how to pass data into methods and return results from them.
The Project: Simple Calculator
You will create a program that adds two numbers together.
The numbers will be passed into a method called addNumbers, and the result will be returned to the main method.
Your output will look like this:
1 2 3 4======================== SIMPLE CALCULATOR The sum of 8 and 12 is 20 ========================
The main method will send the numbers to the method, and the method will send the answer back.
The Concept: Parameters and Return Values
A parameter is a value that you send to a method so it can do something with it.
A return value is what the method gives back after completing its task.
Here’s what that looks like in Java:
1 2 3 4public static int addNumbers(int a, int b) { int sum = a + b; return sum; }
- int a, int b are parameters, they receive the values from the main method.
- return sum; sends the result back to the place where the method was called.
- The word int before the method name means the method will return an integer value.
You can call this method using:
int result = addNumbers(8, 12);
Why This Matters
Passing and returning data makes your programs interactive and flexible.
It allows you to:
- Reuse one method with different values each time.
- Break large problems into smaller steps.
- Make your code easier to read and maintain.
Without parameters and return values, your methods would always work with fixed data, making them less useful.
Let’s Build the Calculator
Here’s the complete program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16public class SimpleCalculator { public static void main(String[] args) { System.out.println("========================"); System.out.println(" SIMPLE CALCULATOR"); int result = addNumbers(8, 12); System.out.println(" The sum of 8 and 12 is " + result); System.out.println("========================"); } public static int addNumbers(int a, int b) { int sum = a + b; return sum; } }
When you run this program, Java will call the addNumbers method, send it two numbers, and display the total.
You can change the numbers inside the parentheses to calculate different sums instantly.
How It Works
- The main method calls addNumbers(8, 12).
- The values 8 and 12 are passed to the parameters a and b.
- The method adds them together and stores the result in sum.
- The method returns sum to the main method.
- The main method prints the result on the screen.
This flow of sending and returning data makes your program much more powerful.
Learn Together with AI
If you are using an AI Copilot, you can easily explore how parameters and return values work by asking questions like:
“Write a Java program that defines a method which takes two numbers as input and returns their sum.”
Then try changing the prompt to learn more:
- “Make it return the product instead of the sum.”
- “Add a method that subtracts one number from another.”
- “Add a method that takes three parameters and returns the average.”
Each new version helps you understand how data flows through methods.
Practice Challenge
Create a program that takes two numbers, multiplies them, and returns the result from a method called multiplyNumbers.
Your output should look like this:
1 2 3 4======================== MULTIPLICATION RESULT The product of 6 and 9 is 54 ========================
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16public class MultiplicationResult { public static void main(String[] args) { System.out.println("========================"); System.out.println(" MULTIPLICATION RESULT"); int result = multiplyNumbers(6, 9); System.out.println(" The product of 6 and 9 is " + result); System.out.println("========================"); } public static int multiplyNumbers(int a, int b) { int product = a * b; return product; } }
When you run this program, it sends two numbers to the method and prints the returned product.
You can now see how easy it is to create methods that can process information and send results back.
Coming Up Next
You have learned how to pass data into methods and return results back to the main program.
Next, you will learn about variable scope, which explains where variables live and how long they stay active inside a program.