Writing methods with parameters and return values
1.6.1 — Writing methods with parameters and return values
A method is a named block of code that you can call when needed. Parameters provide inputs; a return value provides a result.
int holds a whole number. + adds numbers, * multiplies them, and System.out.println(...) prints a value. Each cell contains the method and inputs it needs. The notebook supplies the surrounding class.
Start with the inputs and result
Requirement: add two integers and give back their sum.
- Inputs: two integers.
- Result: one integer.
- Steps: add the inputs, then return the sum.
Writing steps in plain language before coding is called pseudocode. The next example turns those steps into a method named add.
Read the method declaration
In static int add(int first, int second):
staticlets this method be called without creating an object.- The first
intis the return type. addis the method name.int firstandint secondare the parameters.
The braces hold the method’s statements. return gives a value back to the caller and ends this method call.
The values in a call, such as 3 and 4, are arguments. They fill the parameters in order.
Call the method with different inputs
Each call uses the arguments you provide. The next cell declares a method that multiplies two integers, then calls it twice.
A method does not run just because it is declared. It runs when called.
Returning is different from printing
A returned value can be stored and used in another calculation. Returning it does not automatically print it.
The next cell stores the result in result, adds 1, then prints that new value. The method itself has no print statement.
A method that prints instead
void means the method gives back no value. A void method can still perform actions, such as printing.
The next method prints its argument. You call it directly; you cannot store its result in an int variable.
Variables inside a method
A parameter and a variable declared inside a method belong to that call. They do not become variables in the calling code.
The next method uses a local variable named next. The caller receives its value through return.
Return from a chosen branch
A method can use if to choose a result. >= means greater than or equal to. If the first condition below is true, return 0; ends that call immediately.
Otherwise the method subtracts used places from capacity. Every possible path gives back an int.
Practice: return double the input
The next method currently returns its input unchanged. Replace return value; with return value * 2;.
Run the cell. Its print statement should show 10.
Hint
Keep the calculation after return: return value * 2;. Leave the call below the method unchanged.
Worked solution
static int doubleNumber(int value) {
return value * 2;
}
System.out.println(doubleNumber(5));Try another argument
In your completed cell, change only the argument in doubleNumber(...):
| Argument | Expected output |
|---|---|
| 0 | 0 |
| 1 | 2 |
| 5 | 10 |
| -3 | -6 |
Keep the method unchanged. One method can handle all these inputs.
Enable JavaScript to run the Java notebook.