Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
When you call addBonus(7, 3), the method receives the two values that the call supplies. The first argument, 7, matches the first parameter, score. The second argument, 3, matches the second parameter, bonus. Nothing inside addBonus silently discovers these values, and the method cannot assume different input without the caller providing it.
static int addBonus(int score, int bonus) {
return score + bonus;
}
int updatedScore = addBonus(7, 3);The call gives addBonus a precise input: score receives 7 and bonus receives 3. The names score and bonus belong to the method declaration, while 7 and 3 belong to the call. Changing an argument changes the value received by its matching parameter, but the method still follows the same declared input shape: two int values.
During addBonus(7, 3), which values enter the parameters score and bonus?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
The int before addBonus declares that a successful call produces one integer value. In this call, score is 7 and bonus is 3, so the arithmetic expression score + bonus produces 10. The return statement sends that value out of the method, allowing the call itself to stand for the integer 10.
static int addBonus(int score, int bonus) {
return score + bonus;
}
int updatedScore = addBonus(7, 3); // updatedScore becomes 10The return type is a promise Java checks. If addBonus declares int, every path through a successful call must return a compatible int value. Leaving out the return value causes a compile-time error, and returning a value of an incompatible data type also violates the declaration. The type is therefore part of what the caller can rely on, not decoration around the method name.
Printing and returning send information in different directions. Printing 10 writes the characters 10 to the console for a person to see. Returning 10 sends an integer value back to the place where the method call appears. A console display cannot become the value of updatedScore merely because the same digits are visible.
static int addBonus(int score, int bonus) {
System.out.println(score + bonus);
}
int updatedScore = addBonus(7, 3); // compile-time error: no int is returnedThe method above prints 10, but its declaration still promises an int. Because no int reaches the call site, Java rejects the method before it can initialize updatedScore. To fulfill the promise, replace the print operation with a return statement that sends the arithmetic result back to the caller.
static int addBonus(int score, int bonus) {
return score + bonus;
}
int updatedScore = addBonus(7, 3); // the returned value 10 is assignedReplace the print statement inside addBonus with the line that sends 10 back to updatedScore.
static int addBonus(int score, int bonus) {
System.out.println(score + bonus);
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Because addBonus accepts two int arguments and returns an int, its call can appear wherever an int expression is allowed. In int updatedScore = addBonus(7, 3), the call produces 10 on the right side of the assignment. The assignment then stores that returned value in updatedScore. The method is useful here because its input and output are values, not merely text printed for display.
static int addBonus(int score, int bonus) {
return score + bonus;
}
int updatedScore = addBonus(7, 3);
System.out.println(updatedScore); // prints 10Java can check the contract at the call site as well as inside the method. The two supplied arguments must fit the parameter types, and the value returned by the call must fit the type expected by the assignment. With addBonus(7, 3), both inputs are int values and the result is an int, so the call and its assignment agree. If the method had no returned value, or returned an incompatible value, this assignment would not be valid.