ArticleslgStudy

science

Forward declaration

Forward declaration is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Forward declaration rather than just read about it. In short: In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition. It is required for a compiler to know certain properties of an identifier (size for memory allocation, data type for type checking, such as type signature of functions), but it isn't required to kn…

Key takeaways

  • Forward declaration belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Forward declaration to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Forward declaration from memory before moving on to harder problems.

Reference excerpt

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition. It is required for a compiler to know certain properties of an identifier (size for memory allocation, data type for type checking, such as type signature of functions), but it isn't required to know some other details, like the particular value it holds (in case of variables or constants) or definition (in the case of functions). This is particularly useful for one-pass compilers and separate compilation. Forward declaration is used in languages that require declaration before use; it is necessary for mutual recursion in such languages, as it is impossible to define such functions (or data structures) without a forward reference in one definition: one of the functions (respectively, data structures) must be declared first. It is also useful to allow flexible code organization, for example if one wishes to place the main body at the top, and called functions below it. In other languages forward declarations are not necessary, which generally requires instead a multi-pass compiler and for some compilation to be deferred to link time. In these cases identifiers must be defined (variables initialized, functions defined) before they can be employed during runtime without the need for pre-definition in the source code for either compilation or interpretation: identifiers do not need to be immediately resolved to an existing entity.

Examples A basic example in C is void printThisInteger(int);. In C and C++, this represents a forward declaration of a function and is the function's prototype. After processing this declaration, the compiler would allow the program code to refer to the entity printThisInteger() in the rest of the program. The definition for a function must be provided somewhere (same file or other, where it would be the responsibility of the linker to correctly match references to a particular function in one or several object files with the definition, which must be unique, in another):

Variables may have only forward declaration and lack definition. During compilation time these are initialized by language specific rules (to undefined values, 0, null pointers, etc.). Variables that are defined in other source/object files must have a forward declaration specified with a keyword extern:

In Pascal and other Wirth programming languages, it is a general rule that all entities must be declared before use, and thus forward declaration is necessary for mutual recursion, for instance. In C, the same general rule applies, but with an exception for undeclared functions and incomplete types. Thus, in C it is possible (although unwise) to implement a pair of mutually recursive functions thus:

In Pascal, the same implementation requires a forward declaration of second() to precede its use in first(). Without the forward declaration, the compiler will produce an error message indicating that the identifier second() has been used without being declared.

Classes In some object-oriented languages like C++ and Objective-C, it is sometimes necessary to forward-declare classes. This is done in situations when it is necessary to know that the name of the class is a type, but where it is unnecessary to know the structure. In C++, classes and structs can be forward-declared like this:

In C++, classes can be forward-declared if needing to use the pointer-to-that-class type (since all object pointers are the same size, and the size is the metric that the compiler is concerned with). This is especially useful inside class definitions, e.g. if a class contains a member that is a pointer (or a reference) to another class. Forward-declaration is used to avoid unnecessary coupling which help reducing compilation time by reducing the number of header inclusion. This has a triple advantage:

reduce the number of files opened by #include (hence the number of operating system calls) reducing the volume of the pre-processed files (as the header is not included) reducing recompilation impact when the forward declared class is modified. Forward declaration of a class is not sufficient if needing to use the actual class type, for example, a member whose type is that class directly (not a pointer), or if using it as a base class, or using the methods of the class in a method. In Objective-C, classes and protocols can be forward-declared like this:

In Objective-C, classes and protocols can be forward-declared if needing to use them as part of an object pointer type, e.g. Foo* or id<Bar>. This is especially useful inside class definitions, e.g. if a class contains a member that is a pointer to another class; to avoid circular references (i.e. that class might also contain a member that is a pointer to this class), we simply forward-declare the classes instead. Forward declaration of a class or protocol is not sufficient if needing to subclass that class or implement that protocol.

Forward reference The term forward reference is sometimes used as a synonym of forward declaration. However, more often it is taken to refer to the actual use of an entity before any declaration; that is, the first reference to second() in the code above is a forward reference. Thus, we may say that because forward declarations are mandatory in Pascal, forward references are prohibited. An example of (valid) forward reference in C++:

In this example, there are two references to value before it is declared. C++ generally prohibits forward references, but they are allowed in the special case of class members. Since the member function get() cannot be compiled until the compiler knows the type of the member variable value, it is the compiler's responsibility to remember the definition of get() until it sees value's declaration. Permitting forward references can greatly increase the complexity and memory requirements of a compiler, and generally prevents the compiler from being implemented in one pass.

References

Worked examples

Example 1 — a first encounter with Forward declaration

Start with the simplest possible case. Write down what Forward declaration claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Forward declaration before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Forward declaration ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Forward declaration

In research
Forward declaration appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Forward declaration in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Forward declaration is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming constructs, so understanding it makes those chapters shorter.
In everyday life
Look for Forward declaration outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Forward declaration” →

How to study Forward declaration in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Forward declaration means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Forward declaration out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Forward declaration in simple terms?

In computer programming, a forward declaration is a declaration of an identifier (denoting an entity such as a type, a variable, a constant, or a function) for which the programmer has not yet given a complete definition. It is required for a compiler to know certain properties of an identifier (si…

Why does Forward declaration matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Forward declaration?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Forward declaration.

Tags

  • Programming constructs

Keep exploring