All lessons

5. Functions

void functions and scope

0 of 5 activities0%

Reading 1

No return value, local names

Open

Use void when a function does work (like printing) but does not return a value. You call it as a statement: greet(); — not inside cout unless it returns something.

Variables declared inside a function are local. They exist only while that function runs. Two functions can both have a variable named x without conflict.

Prefer small functions that do one clear job.

void greet(string name) {
    cout << "Hello, " << name << endl;
}

int main() {
    greet("Ada");
    return 0;
}

Check 2

Calling void

Open

How do you correctly call void greet(string name)?

Fill in 3

No value returned

Open

The return type for a function that returns nothing is

Try it 4

Print box

Open

Call printBox with different sizes.

main.cpp
Loading editor…
Output will appear here.

Assignment 5

Write shout

Open

Write void shout(string s) that prints s followed by !!! and a newline. In main, read one word and call shout.

main.cpp
Loading editor…