JAVA · ОСНОВЫ · LESSON 18

The method answers.

return · about 14 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Return the number.

When the method should give a result, instead of void indicate the type of response. return returns the number to where it came from.

static int add(int a, int b) {
    return a + b;
}

✦ This method gets two numbers and gives the amount.

STEP 2

Show me the answer.

Call the method inside println. Java first calculates the answer, then shows it.

System.out.println(add(2, 3));
Calculator method
2 and 3entranceaddscore5reply

✦ That's 5.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the signs and brackets.

public class Main {
    public static void main(String[] args) {
        System.out.println(add(2, 3));
    }
    static int add(int a, int b) {
        return a + b;
    }
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create static int multiply (Zint a, int b), which returns the work of a and b. In main output the result multiply(3, 4).

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework