Small Steps VIII — Build with Several Files

Give Each File a Job

Pass values to a function and receive its result.

Separate the score calculation

Name files after their jobs. Create a new ScoreCard project. Keep main.cpp, score.h, and score.cpp in the same folder and replace their contents as follows.

score.h

#pragma once

int add_points(int score, int points);

score.cpp

#include "score.h"

int add_points(int score, int points)
{
    return score + points;
}

main.cpp

#include "score.h"

void small_main()
{
    int score = 0;
    score = add_points(score, 10);
    score = add_points(score, 20);
    print(score);
}

Run Project prints 30. main.cpp controls the program's flow; score.cpp handles the calculation. The score is a local variable in main. Passing values in and returning a new value avoids a shared global variable.

Start with a header named after its source file. Put function declarations in the header and definitions in the source. You do not need a separate file for every small function.

Check the behavior in one file

main.cpp controls the flow while score.cpp handles the calculation.
main.cpp controls the flow while score.cpp handles the calculation.
Example · example.cpp
int add_points(int score, int points)
{
    return score + points;
}

void small_main()
{
    int score = 0;
    score = add_points(score, 10);
    score = add_points(score, 20);
    print(score);
}

Exercise — Check it yourself

Add another call worth 5 points to print 35 in the one-file practice. Then add the same call to main.cpp in your project. Do score.h and score.cpp need to change?

Exercise starter · exercise_starter.cpp
int add_points(int score, int points)
{
    return score + points;
}

void small_main()
{
    int score = 0;
    score = add_points(score, 10);
    score = add_points(score, 20);
    print(score);
}

Hint

Use the menus described above. The solution below is for the one-file practice. Apply the same change to the file with that job in your project.

show solution
int add_points(int score, int points)
{
    return score + points;
}

void small_main()
{
    int score = 0;
    score = add_points(score, 10);
    score = add_points(score, 20);
    score = add_points(score, 5);
    print(score);
}
← Previous lessonNext lesson →