What is the difference between forward declaration and forward reference?
Asked Answered
R

3

16

What is the difference between forward declaration and forward reference?

Forward declaration is, in my head, when you declare a function that isn't yet implemented, but is this incorrect? Do you have to look at the specified situation for either declaring a case "forward reference" or "forward declaration"?

Radburn answered 30/3, 2009 at 9:29 Comment(0)
F
5

From Wikipedia:

Forward Declaration

Declaration of a variable or function which are not defined yet. Their defnition can be seen later on.

Forward Reference

Similar to Forward Declaration but where the variable or function appears first the definition is also in place.

Folder answered 30/3, 2009 at 9:40 Comment(2)
Oh. So declaration is when you declare something, use it and then implement it. Reference is when you use, and then declare it?Radburn
Yes. In terms of function references, the definition should have been done before use.Folder
P
35

A forward declaration is the declaration of a method or variable before you implement and use it. The purpose of forward declarations is to save compilation time.

The forward declaration of a variable causes storage space to be set aside, so you can later set the value of that variable.

The forward declaration of a function is also called a "function prototype," and is a declaration statement that tells the compiler what a function’s return type is, what the name of the function is, and the types its parameters. Compilers in languages such as C/C++ and Pascal store declared symbols (which include functions) in a lookup table and references them as it comes across them in your code. These compilers read your code sequentially, that is, top to bottom, so if you don't forward declare, the compiler discovers a symbol that it can't reference in the lookup table, and it raises an error that it doesn't know how to respond to the function.

The forward declaration is a hint to the compiler that you have defined (filled out the implementation of) the function elsewhere.

For example:

int first(int x); // forward declaration of first

...

int first(int x) {
   if (x == 0) return 1;
   else return 2;
}

But, you ask, why don't we just have the compiler make two passes on every source file: the first one to index all the symbols inside, and the second to parse the references and look them up? According to Dan Story:

When C was created in 1972, computing resources were much more scarce and at a high premium -- the memory required to store a complex program's entire symbolic table at once simply wasn't available in most systems. Fixed storage was also expensive, and extremely slow, so ideas like virtual memory or storing parts of the symbolic table on disk simply wouldn't have allowed compilation in a reasonable timeframe... When you're dealing with magnetic tape where seek times were measured in seconds and read throughput was measured in bytes per second (not kilobytes or megabytes), that was pretty meaningful.

C++, while created almost 17 years later, was defined as a superset of C, and therefore had to use the same mechanism.

By the time Java rolled around in 1995, average computers had enough memory that holding a symbolic table, even for a complex project, was no longer a substantial burden. And Java wasn't designed to be backwards-compatible with C, so it had no need to adopt a legacy mechanism. C# was similarly unencumbered.

As a result, their designers chose to shift the burden of compartmentalizing symbolic declaration back off the programmer and put it on the computer again, since its cost in proportion to the total effort of compilation was minimal.

In Java and C#, identifiers are recognized automatically from source files and read directly from dynamic library symbols. In these languages, header files are not needed for the same reason.

A forward reference is the opposite. It refers to the use of an entity before its declaration. For example:

int first(int x) {
   if (x == 0) return 1;
   return second(x-1); // forward reference to second
}

int second(int x) {
   if (x == 0) return 0;
   return first(x-1);
}

Note that "forward reference" is used sometimes, though less often, as a synonym for "forward declaration."

Parthenon answered 21/1, 2012 at 21:47 Comment(0)
F
5

From Wikipedia:

Forward Declaration

Declaration of a variable or function which are not defined yet. Their defnition can be seen later on.

Forward Reference

Similar to Forward Declaration but where the variable or function appears first the definition is also in place.

Folder answered 30/3, 2009 at 9:40 Comment(2)
Oh. So declaration is when you declare something, use it and then implement it. Reference is when you use, and then declare it?Radburn
Yes. In terms of function references, the definition should have been done before use.Folder
T
3

forward declarations are used to allow single-pass compilation of a language (C, Pascal).

if forward references are allowed without forward declaration (Java, C#), a two-pass compiler is required.

Thanatos answered 30/3, 2009 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.