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.
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);
What is a function prototype?
What is a valid prototype for this function?
float Calculate(int x, float y) {
return x * y;
}
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;
}
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!
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way