Parameters and return values
What you will learn
Parameters let a method accept inputs. A return value lets it provide a result to the caller. For example, a single price calculation can work for many quantities instead of reading one fixed variable or printing its own answer.
public class Main { static int total(int quantity, int unitPrice) { return quantity * unitPrice; } public static void main(String[] args) { int cost = total(3, 7); System.out.println(cost); System.out.println(total(2, 5) + 1); }}Expected output:
2111The first result is stored; the second result participates in an addition.
How it works
In static int total(int quantity, int unitPrice), quantity and unitPrice are parameters. In total(3, 7), 3 and 7 are arguments. The argument expressions are evaluated left to right, and their values initialize the parameters. The declared result type is int; return quantity * unitPrice; ends that invocation and supplies its result. Every reachable normal completion path of a value-returning method must be prevented from falling off its end. A void method may use return; for an early exit. A method can return one value, which may itself be an object. Returning a value does not print it. Keep calculations separate from display when they should be reusable.
public class Main { static boolean isValid(int quantity) { return quantity >= 0 && quantity <= 100; } public static void main(String[] args) { System.out.println(isValid(0)); System.out.println(isValid(100)); System.out.println(isValid(101)); }}Expected output:
truetruefalseBoth endpoints are allowed. The method is a predicate: it answers a yes/no question.
Check a boundary or unusual input
public class Main { static void printCount(int count) { if (count < 0) { return; } System.out.println(count); } public static void main(String[] args) { printCount(-1); printCount(0); printCount(2); }}Expected output:
02The first call returns without printing; zero is a valid count.
Common mistakes
Printing inside a method does not substitute for returning its declared result. Do not ignore a returned value if your next operation depends on it. A method returning int cannot return a boolean; an if without a fallback may leave a path with no return. In these examples inputs are bounded; production totals need explicit overflow handling.
Try it yourself
Write static int shipping(int quantity) for 0–20 items. Return 0 for zero, 5 for 1–9, and 0 for 10 or more (fictional free shipping). Print only the result for a chosen input. Test 1, 0, 9, 10 and 20.
Save each example independently as Main.java. Compile with javac Main.java and run with java Main. Local Java practice only; automatic ByteMind Java grading is not established.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Parameters and return values — knowledge check
Apply parameters and return values to examples and boundary conditions.
In total(3, 7), what is 7?
What does return 12; do inside an int method?
Which type is appropriate for a yes/no validator?
Can a void method use return;?
A non-void method has a reachable path falling off the end. What happens?