While going through a book on machine instructions and programs I came across a particular point which says that an assembler scans an entire source program twice. It builds a symbol table during the 1st pass/scan and associates the entire program with it during the second scan. The assembler needs to provide an address in a similar way for a function.
Now, since the assembler passes through the program twice, why is it necessary to declare a function before it can be used? Wouldn't the assembler provide an address for the function from the 1st pass and then correlate it to the program during the 2nd pass ?
I am considering C programming in this case.

- 14,226
- 3
- 42
- 60

- 375
- 1
- 4
- 16
-
2In what assembler do you need to declare a function before it can be used? – Jose Manuel Abarca Rodríguez Nov 30 '16 at 18:50
-
4C compilers are not assemblers. The rules of the C language having nothing to do with how assemblers work. – John Bollinger Nov 30 '16 at 18:51
-
What I meant is that, the compiled program, which is to be "assembled" requires proper definition for each and every function,variable etc that the program contains.If the assembler had to run through the program twice, wouldn't the need of declaring functions be unnecessary ? – mahesh Rao Nov 30 '16 at 18:59
-
1You're confusing the compiler and an assembler. They're not the same thing. – Ken White Nov 30 '16 at 19:03
-
The second pass is only needed to resolve (*fixup*) the forward references. It need not even be a complete pass over the source, just over the list of references. IIRC the Borland stran of compilers used a similar technique to implement the Turbo strain of "compilers". – wildplasser Nov 30 '16 at 19:07
-
For example, consider a c program that has been compiled.Now, if the program has a function with no declaration, wouldn't the program be compiled in such a way, where the first reference to the function comes after say, main ? If it does, wouldn't the assembler, which passes through the program, make a reference of the function when it's converting the program ? Or would it not work that way ? – mahesh Rao Nov 30 '16 at 19:11
-
There is usually no assembler pass of C source. It is compiled. – Weather Vane Nov 30 '16 at 19:12
-
You need to go back to your book and see if it has a section on compilers that explains the differences between a compiler and an assembler. – Ken White Nov 30 '16 at 19:21
-
Apologies, as I missed out a part. Silly mistake. – mahesh Rao Nov 30 '16 at 19:24
1 Answers
The simple answer is that C programs require that functions be declared before it can be used because the C language was designed to be processed by a compiler in a single pass. It has nothing to with assemblers and addresses of functions. The compiler needs to know the type of a symbol, whether its a function, variable or something else, before it can use it.
Consider this simple example:
int foo() { return bar(); }
int (*bar)();
In order to generate the correct code the compiler needs to know that bar
isn't a function, but a pointer to a function. The code only works if you put extern int (*bar)();
before the definition of foo
so the compiler knows what type bar
is.
While the language could have been in theory designed to require the compiler to use two passes, this would have required some significant changes in the design of the language. Requiring two passes would also increase the required complexity of the compiler, decreasing the number of platforms that could host a C compiler. This was very important consideration back in the day when C was first being developed, back when 64K (65,536) bytes of RAM was a lot of memory. Even today would have noticeable impact on the compile times of large programs.
Note that the C language does sort of allows what you want anyways, by supporting implicit function declarations. (In my example above this it what happens in foo
when bar
isn't declared previously.) However this feature is obsolete, limited, and considered dangerous.

- 38,414
- 7
- 81
- 112