C++ · ОСНОВЫ · LESSON 17

Function with response

return and arguments · about 12 minutes

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

How it works.

In the brackets of the function, specify the input numbers. return gives the result to the code.

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

✦ add(2, 3) will return 5.

STEP 2

Try to understand.

add(2, 3) will return 5.

cout << add(2, 3) << endl;

✦ Compare this to the first one. Then do the cool job yourself.

IN-CLASS PRACTICE

Type the code yourself

Manually type the program from the sample. So you'll remember commands and brackets.

#include <iostream>
using namespace std;

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

int main() {
    cout << add(2, 3) << endl;
    return 0;
}
Open the editor for class work
HOMEWORK

Solve it yourself

Create a function int multiply (Zint a, int b), which returns the work. From main output multiply(3, 4).

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

Opening up your homework