Sub topic 10
Methods: Parameters and Return Values
So far, your programs have run line by line, top to bottom, inside main. Real programs need to reuse the same steps in many places. A method is a named block of code that does one job and can be called whenever you need it. In this chapter you will learn how to write methods, pass information into them, and get results back out.
Why Methods Matter
Methods help you:
- Avoid copying the same code into several places.
- Give a name to a step, which makes programs easier to read.
- Test and fix one piece of logic in one place.
Anatomy of a Method
A method has a few parts. Here is a simple one:
public class Greeter {
static void greet() {
System.out.println("Hello!");
}
public static void main(String[] args) {
greet(); // call the method
}
}Let's name each part:
static— for now, this letsmaincall the method without creating an object. You will learn more about it later in the tutorial.void— the return type, the kind of value the method gives back.voidmeans "returns nothing."greet— the method's name. You choose it; use a verb that describes the job.( )— the parameter list. This one is empty, so the method needs no input.{ ... }— the body, the code that runs on each call.
Parameters: Passing Information In
A parameter is a variable that a method accepts. You list it inside the parentheses. The value you supply when calling is called an argument.
public class Greeter {
static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
public static void main(String[] args) {
greet("Ada"); // argument "Ada" fills the parameter name
greet("Bo");
}
}Output:
Hello, Ada!
Hello, Bo!A method can take several parameters, separated by commas. Each parameter needs a type:
static void printSum(int a, int b) {
System.out.println(a + b);
}Call it with printSum(3, 4); and it prints 7.
Return Values: Getting Information Out
A method can hand a value back to the caller with the return statement. When a method returns a value, its return type must match that value's type.
public class Calculator {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(2, 3); // store what add() returns
System.out.println(result); // prints 5
System.out.println(add(10, 20)); // prints 30
}
}Here the return type is int, so the method must return an int. Once a return statement runs, the method stops immediately — no further lines in its body execute.
A common beginner mistake: writing int add(int a, int b) { a + b; } with no return. That will not compile. Every path through a method with a non-void return type must return a matching value.
Callouts like this one will flag common pitfalls throughout the tutorial:
A Worked Example: Combining Both
Here is a small program that uses two methods — one that takes parameters and returns a value, and one that uses the result:
public class Discount {
// Returns the price after applying a percentage discount.
static double applyDiscount(double price, double percent) {
return price - (price * percent / 100);
}
static void showFinalPrice(double price, double percent) {
double finalPrice = applyDiscount(price, percent); // use the returned value
System.out.println("Final price: " + finalPrice);
}
public static void main(String[] args) {
showFinalPrice(100.0, 20.0); // prints 80.0
}
}Notice how showFinalPrice calls applyDiscount and stores its return value in a variable. Methods can call other methods — this is how larger programs are built from small, readable pieces.
How It Fits Together
Key Points
- A method is a named, reusable block of code.
- Parameters are the typed inputs a method declares; arguments are the actual values you pass.
- The return type says what a method gives back;
voidmeans nothing is returned. returnboth hands back a value and ends the method immediately.- Methods can call other methods, letting you compose programs from small parts.
Practice
Exercise
What is wrong with this method, and how would you fix it? static double half(int n) { n / 2; }
Show answer
The body computes n / 2 but never returns it. Fix: static double half(int n) { return n / 2.0; }. (Using 2.0 also avoids integer division, which would truncate 5 / 2 to 2.)
Exercise
Write a method called max that takes two int parameters and returns the larger one.
Show answer
static int max(int a, int b) { if (a >= b) { return a; } else { return b; } } — every path returns an int, as required.