All lessons

5. Functions

Parameters and return values

0 of 5 activities0%

Reading 1

Inputs and outputs of a function

Open

Parameters are the variables in the function header. Arguments are the values you pass when you call it.

The return type says what comes back. return expression; ends the function and provides that value.

A function can take multiple parameters: int max2(int a, int b).

int max2(int a, int b) {
    if (a > b) return a;
    return b;
}

int main() {
    cout << max2(3, 8) << endl;  // 8
    return 0;
}

Check 2

Argument vs parameter

Open

In max2(3, 8), what are 3 and 8?

Fill in 3

Sending a result

Open

The keyword that sends a value back to the caller is

Try it 4

max2

Open

Call max2 with other pairs.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Write triple

Open

Write int triple(int x) that returns 3 * x. In main, read one int and print triple(n).

main.cpp
Loading editor…