Search

1.7 — Forward declarations and definitions

Take a look at this seemingly innocent sample program called add.cpp:

You would expect this program to produce the result:

The sum of 3 and 4 is: 7

But in fact, it doesn’t compile at all! Visual Studio 2005 Express produces the following compile errors:

add.cpp(5) : error C3861: 'add': identifier not found
add.cpp(9) : error C2365: 'add' : redefinition; previous definition was 'formerly unknown identifier'

The reason this program doesn’t compile is because the compiler reads files sequentially. When the compiler reaches the function call to add() on line 5 of main(), it doesn’t know what add is, because we haven’t defined add() until line 9! That produces the first error (“identifier not found”).

When Visual Studio 2005 gets to the actual declaration of add() on line 9, it also complains about add being redefined. This is somewhat misleading, given that it wasn’t ever defined in the first place. Later versions of Visual Studio correctly omit this additional error message.

Despite the redundancy of the second error, it’s useful to note that it is fairly common for a single error to produce (often redundant) multiple compiler errors or warnings.

Rule: When addressing compile errors in your programs, always resolve the first error produced first.

To fix this problem, we need to address the fact that the compiler doesn’t know what add is. There are two common ways to address the issue.

Option 1: Reorder the function calls so add() is defined before main():

That way, by the time main() calls add(), the compiler will already know what add() is. Because this is such a simple program, this change is relatively easy to do. However, in a larger program, it can be tedious trying to figure out which functions call which other functions (and in what order) so they can be declared sequentially.

Furthermore, this option is not always possible. Let’s say we’re writing a program that has two functions A and B. If function A calls function B, and function B calls function A, then there’s no way to order the functions in a way that they will both be happy. If you define A first, the compiler will complain it doesn’t know what B is. If you define B first, the compiler will complain that it doesn’t know what A is.

Function prototypes and forward declaration of functions

Option 2: Use a forward declaration.

A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier.

In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function’s body. This way, when the compiler encounters a call to the function, it’ll understand that we’re making a function call, and can check to ensure we’re calling the function correctly, even if it doesn’t yet know how or where the function is defined.

To write a forward declaration for a function, we use a declaration statement called a function prototype. The function prototype consists of the function’s return type, name, parameters, but no function body (the part between the curly braces). And because the function prototype is a statement, it ends with a semicolon.

Here’s a function prototype for the add() function:

Now, here’s our original program that didn’t compile, using a function prototype as a forward declaration for function add():

Now when the compiler reaches add() in main, it will know what add() looks like (a function that takes two integer parameters and returns an integer), and it won’t complain.

It is worth noting that function prototypes do not need to specify the names of the parameters. In the above code, you can also forward declare your function like this:

However, we prefer to name our parameters (using the same names as the actual function), because it allows you to understand what the function parameters are just by looking at the prototype. Otherwise, you’ll have to locate the function definition.

Tip: You can easily create function prototypes by using copy/paste on your function declaration. Don’t forget the semicolon on the end.

Forgetting the function body

One question many new programmers have is: what happens if we forward declare a function but do not define it?

The answer is: it depends. If a forward declaration is made, but the function is never called, the program will compile and run fine. However, if a forward declaration is made, the function is called, but the program never defines the function, the program will compile okay, but the linker will complain that it can’t resolve the function call.

Consider the following program:

In this program, we forward declare add(), and we call add(), but we never define add() anywhere. When we try and compile this program, Visual Studio 2005 Express produces the following message:

Compiling...
add.cpp
Linking...
add.obj : error LNK2001: unresolved external symbol "int __cdecl add(int,int)" (?add@@YAHHH@Z)
add.exe : fatal error LNK1120: 1 unresolved externals

As you can see, the program compiled okay, but it failed at the link stage because int add(int, int) was never defined.

Other types of forward declarations

Forward declarations are most often used with functions. However, forward declarations can also be used with other identifiers in C++, such as variables and user-defined types. Other types of identifiers (e.g. user-defined types) have a different syntax for forward declaration.

We’ll talk more about how to forward declare other types of identifiers in future lessons.

Declarations vs. definitions

In C++, you’ll often hear the words “declaration” and “definition” used. What do they mean? You now have enough of a framework to understand the difference between the two.

A definition actually implements or instantiates (causes memory to be allocated for) the identifier. Here are some examples of definitions:

You can only have one definition per identifier. A definition is needed to satisfy the linker.

A declaration is a statement that announces an identifier (variable or function name) and its type. Here are some examples of declarations:

A declaration is all that is needed to satisfy the compiler. This is why using a forward declaration is enough to keep the compiler happy. However, if you forget to include the definition for the identifier, the linker will complain.

You’ll note that “int x” appears in both categories. In C++, all definitions also serve as declarations. Since “int x” is a definition, it’s by default a declaration too. This is the case with most declarations.

However, there are a small subset of declarations that are not definitions, such as function prototypes. These are called pure declarations. Others types of pure declarations include forward declarations for variables, class declarations, and type declarations (you will encounter these in future lessons, but don’t need to worry about them now). You can have as many pure declarations for an identifier as you desire (although having more than one is typically redundant).

Quiz

1) What’s the difference between a function prototype and a forward declaration?

2) Write the function prototype for this function:

3) For each of the following programs, state whether they fail to compile, fail to link, or compile and link. If you are not sure, try compiling them!

4)

5)

6)

Quiz Answers
1) Show Solution

2) Show Solution

3) Show Solution

4) Show Solution

5) Show Solution

6) Show Solution

1.8 -- Programs with multiple files
Index
1.6 -- Whitespace and basic formatting

175 comments to 1.7 — Forward declarations and definitions

  • African

    Can you explain what’s the difference between not compiling and not linking please. 🙂

  • DonK

    In Solution 5, you indicate the problem is the difference in number of parameters between the function itself and its declaration. If I add a parameter to the declaration, "int z" so it matches the number of parameter in the function, the code still won’t compile.

    If I change the main function from

    to

    passing a third argument, "5", to the function, the code compiles.

  • Paulo

    I want some help to understand this

    can you just explain better to me?

    • Alex

      Sure. Function add() is defined to take two parameters, which we’re calling x and y (they have to have some name, so we can differentiate them inside the function). When we call function add(), we’ll pass it two arguments -- the first argument will be associated with x, and the second argument will be associated with y. So when we call add(3, 4), the value of 3 is passed to x, and the value of 4 is passed to y.

  • Scott

    Is that second error message really redundant though? I understand that it’s a little confusing, but it seems to make logical sense to me. When the compiler first sees "add", it recognizes that add is undefined and complains, but at that point add IS defined as an "unknown identifier". So, when the compiler gets to line 9, now the compiler complains that it has already defined add as an unknown identifier, so it doesn’t want to redefine it.
    I’m not complaining that this error message got removed or anything, and I understand that it is confusing… It just seems to me that the message DOES make sense in a roundabout sort of way, and it also seems to me to be technically correct. I am also not suggesting that you should rewrite any of this page, I just wanted to get confirmation from you on whether or not I’m thinking about this in the right way. In any case, thank you again for all your hard work on this course, it has been extremely useful for me! You’re the man Alex!

    • Alex

      I’m not sure I really understand why older versions of Visual Studio defined an unknown identifier as “unknown identifier” rather than just leaving it undefined. It makes more sense to leave it undefined so that when it was defined it doesn’t cause a redefinition error (because it’s not a redefinition, it’s the actual definition).

  • David

    I tried experimenting with function prototypes, and I found this a bit weird. I know how you said that all function prototypes need to do is tell the return type and parameters, but you can just use different identifiers for the parameters and retain the parameters’ datatypes.

    Is this worth a comment?

    • Alex

      I updated the lesson to note that you should use the same parameter names for both the actual function and prototype (even though C++ lets you do otherwise). For prototype purposes, the compiler ignores the names, so they’re really there just for the benefit of the programmer.

  • Cody

    Hi Alex,
    could you help me understand better about declaring and defining?

    does this define the variable x as well as declare it?
    Is defining different from instantiating?

    would this also be true?

    thanks!

    • Alex

      This declares and defines variable x. It also has the effect of causing x to be instantiated.

      Assuming these are meant to be separate questions (not sequential statements in a program), both do the same thing: declare, define, and instantiate variable x. The former one also initializes x, whereas the latter one does not.

  • My dear c++ Teacher,
    Please let me ask that I can not understand:
    For program forgetting the function body, Code:Blocks’s Build massages among others are:
    undefined reference to ‘add(int, int)’
    error: 1d returned 1 exit status
    === Build failed: 2 error(s), etc.
    I understand there is one error: undefined reference to ‘add(int, int)’.
    What is second error?

    • Alex

      No idea. I only see one error there.

      • My dear c++ Teacher,
        Please let me say my suspicion.
        Code::Blocks by "2 errors" mean one error in two files: add.obj and add.exe, as visual studio 2005 express’s message. Do you think so?
        By the way let me say my answer to quizzes 3 and 4:
        The compiler will complain that the add() called in main() does not have the same number of ARGUMENTS as the number of PARAMETERS of the one that was forward declared. Is it correct?

        With regards and friendship.

  • My dear c++ Teacher,
    Please let me say what I can not understand. You say:
    "A definition actually implements or instantiates (causes memory to be allocated for) the identifier"
    My understanding is that variable declaration causes memory to be allocated for the identifier.

    With regards and friendship.

    • Alex

      Incorrect. With variables, most of the time, the variable definition serves as the declaration as well. For example:

      This statement is both a declaration (it tells the compiler about a variable named x of type int) and a definition (when executed it causes the compiler to allocate memory).

      It’s possible to have variable declaration-only statements:

      This tells the compiler that there is a variable named x, but that the variable already exists somewhere else (e.g. in another file), so it should not allocate memory for it. We cover this in chapter 4.

      • My dear c++ Teacher,

        Please accept my many thanks for you replied my message. My knowledge so far is that compiler does not involve in execution. Executable file allocates memory.

        With regards and friendship.

        • Alex

          When I say, “the compiler allocates memory”, I’m speaking imprecisely to mean, “The compiler will create a statement that when executed will cause memory to be allocated”.

  • Alex

    Hi Alex,
    You listed the following as a reason for forward declarations:

    "If function A calls function B, and function B calls function A, then there’s no way to order the functions in a way that they will both be happy. If you define A first, the compiler will complain it doesn’t know what B is. If you define B first, the compiler will complain that it doesn’t know what A is."

    However, how can two functions ever call each other without producing a continuous repeating cycle. For example:

    I’d be ever so grateful if you could provide me with an example where a cycle doesn’t form when two functions call each other. This would better my understanding.

    Many thanks Alex, this site has helped me massively.

    Alex

    • Alex

      In short, the answer is that one or both of your functions can have code that prevents the other function from being called if some other condition is true.

      Here’s a short example with a single function that you can extrapolate to the two function case:

      We cover this in more detail in the lessons in chapter 7 talking about recursion.

  • Val

    The function call of add is on line 5…..not 6. And add is defined at line 9 not 10 -(3rd paragraph). First example.

    Great website !!! I’m really finding it useful and enjoying it.

    Thanks

    Val

  • My dear c++ Teacher,
    Please comment following program. It works fine.

    With regards and friendship.

    • Alex

      I’m not sure what you want me to comment on, other than to say this program is hard to read because the function definitions are on the same line as the function prototypes.

      • My dear c++ Teacher,
        Please let me ask a clarification:
        Do you mean: main, f1, and f2, bodies are on the same line as the their types, names and parameters?
        My understanding is that f1 and f2 prototypes are above main() function, and their definitions below it.

        • Alex

          I mean the function bodies are on the same line as the function prototype. E.g. this:

          Instead of this:

  • My dear c++ Teacher,
    Please let me again ask you:
    Is it correct that "function prototype is a forward declaration for a function"?
    With regards and friendship.

  • My dear c++ Teacher,
    Please let me this question:
    In subsection "Declarations vs. definitions", 3rd paragraph, you state:
    "A declaration is a statement that defines an identifier (variable or function name) and its type.". Do you mean "… that declares an identifier …"?
    With regards and friendship.

    • Alex

      Poor wording on my part. I changed “defines” to “announces”. A declaration announces an identifier and its type. A definition instantiates or implements it.

  • My dear c++ Teacher,
    Please let me point out that in first paragraph you state:
    "The reason this program doesn’t compile is because the compiler reads files sequentially".
    Apparently you mean: "reads lines sequentially".
    Also in next sentence you state: "When the compiler reaches the function call to add() on line 6 of main(),".
    In PC I use, function call to add() is on line 5.
    With regards and friendship.

    • Alex

      It reads both files sequentially and the lines in the files sequentially. Your line numbers may vary slightly depending on whitespacing and whether you #include “stdafx.h” or not.

  • My dear c++ Teacher,
    Please let me point out that in "Function prototypes and forward declaration of functions" subsection, under program, you state:
    "Now when the compiler reaches add() in main, it will know what add() looks like (a function that takes two integer parameters and returns an integer), and it won’t complain."
    When compiler reaches add(), in main(), not only will know what add() looks like, but also will execute it, although it is AFTER main().
    With regards and friendship.

    • Alex

      No, that isn’t quite right. Remember, the compiler just does syntax checking -- the function isn’t actually executed until runtime.

      • My dear c++ Teacher,
        Please let me say that I am forced to use compilers online that compile, link, and run, altogether, by one click. One exception is
        https://www.tutorialspoint.com/compile_cpp_online.php
        that compiles (and apparently links) by clicking on "Compile", and executes by clicking on "Execute".
        With regards and friendship.

  • nopenope

    Maybe u should provide code tags for the second solution.

  • Kaleb

    You can tell the time and effort they put into this tutorial is outstanding! Great job, and very helpful 🙂

  • Richard

    I’m a bit confused about the relation between the terms “variable definition” and “variable instantiation”.

    Is my understanding of "definition" correct?
    "Definition is a more general term covering both the implementation (of a function for example) and the instantiation (which actually allocates some memory) of the identifier"

    • Alex

      A definition is a general term for the code that uniquely specifies how an identifier is implemented. It’s what the linker needs to work.

      For variables a definition causes an instantiation. For functions and types, the definition causes those things to become callable/usable.

  • Amryt

    #include <iostream>
    using namespace std;

    int add(int x, int y);

    int main()
    {
        int a , b;
      
         cout<<add(3,4)<<endl;
        
      
         cin>> a >> b >> endl;
          cout<<add(a,b)<<endl;

        return 0;
    }
        
    int add(int x , int y)
    {
        return x+y;
    }
        

    I tried adding cin fucntion so that ,  a value can be added at the output itself. why isn’t it working?
    what is wrong with the prog?

    Usind Dev c++

  • sabrina

    ""Now, here’s our original program that didn’t compile, using a function prototype as a forward declaration for function add():

    #include <iostream>

    int add(int x, int y); // forward declaration of add() (using a function prototype)

    int main()
    {
        using namespace std;
        cout << "The sum of 3 and 4 is: " << add(3, 4) << endl; // this works because we forward declared add() above
        return 0;
    }

    int add(int x, int y) // even though the body of add() isn’t defined until here
    {
        return x + y;
    }""

    I used the same example, but they give me an error in "int main()" and they say that it is undefined. I am confused !!!

  • Harry

    "It is worth noting that function", "noting" should be "nothing". 🙂

    • Alex

      No, it’s correct as written. Noting as in “taking a note”.

    • Darren

      "It is worth nothing that function" cried Father. "I raised him from a babe, tried to teach him right from wrong, pass-by-const-reference rather than by-value, gave him a blueprint, a prototype to work form, and what does he give me in return? Nothing"

      (I think I’ve had too much coffee … and sugar)

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter