Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do functions need to be declared before they are used?

Tags:

When reading through some answers to this question, I started wondering why the compiler actually does need to know about a function when it first encounters it. Wouldn't it be simple to just add an extra pass when parsing a compilation unit that collects all symbols declared within, so that the order in which they are declared and used does not matter anymore?

One could argue, that declaring functions before they are used certainly is good style, but I am wondering, is there are any other reason why this is mandatory in C++?

Edit - An example to illustrate: Suppose you have to functions that are defined inline in a header file. These two function call each other (maybe a recursive tree traversal, where odd and even layers of the tree are handled differently). The only way to resolve this would be to make a forward declaration of one of the functions before the other.

A more common example (though with classes, not functions) is the case of classes with private constructors and factories. The factory needs to know the class in order to create instances of it, and the class needs to know the factory for the friend declaration.

If this is requirement is from the olden days, why was it not removed at some point? It would not break existing code, would it?

like image 1000
Björn Pollex Avatar asked Jan 21 '11 10:01

Björn Pollex


People also ask

Why do functions need to be declared?

So a declaration of a function on the top tells the compiler that a function exists in the program which has to be executed. Also remember the execution is done in a top to bottom manner.

Why does a function need to be defined before it is called?

So I just got the answer from a senior programmer. It has to do with the difference between interpreted and compiled languages. JavaScript is an interpreted language: the interpreter runs and executes the program sequentially, therefore, functions have to be defined before they're called.

Can we use a function without declaring it?

You can't call a function without declaring it first.

Do functions have to be defined before they are called in Python?

All functions must be defined before any are used. However, the functions can be defined in any order, as long as all are defined before any executable code uses a function.


2 Answers

How do you propose to resolve undeclared identifiers that are defined in a different translation unit?

C++ has no module concept, but has separate translation as an inheritance from C. A C++ compiler will compile each translation unit by itself, not knowing anything about other translation units at all. (Except that export broke this, which is probably why it, sadly, never took off.)
Header files, which is where you usually put declarations of identifiers which are defined in other translation units, actually are just a very clumsy way of slipping the same declarations into different translation units. They will not make the compiler aware of there being other translation units with identifiers being defined in them.

Edit re your additional examples:
With all the textual inclusion instead of a proper module concept, compilation already takes agonizingly long for C++, so requiring another compilation pass (where compilation already is split into several passes, not all of which can be optimized and merged, IIRC) would worsen an already bad problem. And changing this would probably alter overload resolution in some scenarios and thus break existing code.

Note that C++ does require an additional pass for parsing class definitions, since member functions defined inline in the class definition are parsed as if they were defined right behind the class definition. However, this was decided when C with Classes was thought up, so there was no existing code base to break.

like image 161
sbi Avatar answered Oct 13 '22 05:10

sbi


Historically C89 let you do this. The first time the compiler saw a use of a function and it didn't have a predefined prototype, it "created" a prototype that matched the use of the function.

When C++ decided to add strict typechecking to the compiler, it was decided that prototypes were now required. Also, C++ inherited the single-pass compilation from C, so it couldn't add a second pass to resolved all symbols.

like image 43
MarkH Avatar answered Oct 13 '22 06:10

MarkH