Small Steps I — Creating with Text and Numbers

Naming a task you created

Call greet three times.

What you will learn

A function groups instructions under a name. You have used print; now you will create a function named greet yourself.

Try it

Example · example.cpp
void greet()
{
    print("Hello!");
}

void small_main()
{
    greet();
    greet();
}

The upper section is the definition of what greet does. greet(); below is a call that asks it to do that work. A definition alone does not run. Calling it from small_main goes to greet, then returns after it finishes.

void means the function does not return a result value to its caller. It can still produce output. Empty () means that no values are passed in.

Expected output:

Hello!
Hello!

Exercise

Call greet three times.

Exercise starter · exercise_starter.cpp
void greet()
{
    print("Hello!");
}

void small_main()
{
    greet();
    greet();
}

Hint

Change the parts of the example that you need.

show solution
void greet()
{
    print("Hello!");
}

void small_main()
{
    greet();
    greet();
    greet();
}
← Previous lessonNext lesson →