Forward Declarations in C++

Understand what function prototypes are, and learn how we can use them to let us order our code any way we want.
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a technician character
Ryan McCombe
Ryan McCombe
Posted

So far, we've been working under the restrictions that functions must be defined before they are used. For example, we've been unable to create code like shown below:

int main() {
  // Add is not defined yet
  Add(1, 2);
}

int Add(int x, int y) {
  return x + y;
}

Because the compiler reads our files top-to-bottom, when it reaches line 3, it encounters a call to a function that has not been defined.

The easy solution we've been using has been to move Add above main. However, we may not want to do this for stylistic reasons. Worse, not all problems of this category can be solved by just moving functions around.

Instead, we can forward declare a function. This is a promise to the compiler that the thing we're trying to use is going to be defined somewhere else. Once all the code is linked together, everything will be resolved.

We'll discuss how our code gets built in a later chapter, and what this "linking" process actually is. For now, lets see how we can forward declare a function, and get our code working.

Function Prototypes in C++

To forward declare a function, we first need to understand what a function prototype is. This is sometimes also called the signature. We can think of prototype as just the heading of the function. In other words, the function without its body. For example, if we have the following Add function:

int Add(int x, int y) {
  return x + y;
}

Its prototype would be this:

int Add(int x, int y);

So, the prototype is the function's return type, the function name, and the parameter types.

In this example, the return type is int, the name is Add, and the parameter types are int, int

Within the function prototype, the names of our parameters are optional. They are typically worth including anyway for the benefit of anyone reading our code.

We can infer a lot more about the function just from its prototype if the prototype includes the names of the parameters.

But technically, only the type is relevant for the prototype, so the following is also a valid prototype for our function:

int Add(int, int);

Test your Knowledge

What is a function prototype?

What is a valid prototype for this function?

float Calculate(int x, float y) {
  return x * y;
}

Using Forward Declaration

Now, we no longer have to define our function before we use it - we just have to declare its prototype before we use it.

Prototypes are typically put near the top of the files that use them, but as long as they're provided before the functions are used, our code will work as expected:

int Add(int x, int y);

int main() {
  Add(1, 2);
}

int Add(int x, int y) {
  return x + y;
}

Test your Knowledge

After running the code below, what is the value of x inside the main function?

int main() {
  int x { Calculate() };
}

int Calculate() {
  return 10 * 10;
}

After running the code below, what is the value of x inside the main function?

int Calculate();

int main() {
  int x { Calculate() };
}

int Calculate() {
  return 10 * 10;
}

This separation between declaring a function and defining it is likely to feel quite strange, especially at this stage.

However, this is a very important concept. In fact, most C++ code is created this way. This is because the seperation can have some major workflow benefits for larger projects. We'll see why that is very soon.

For now, lets round off this section by introducing loops!

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

Creating Loops in C++

Learn how we can use loops to iterate over a block of code, executing it as many times as needed.
3D art showing a dragon monster
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved