Click to See Complete Forum and Search --> : C++ functions


yawningdog
01-21-2002, 09:32 PM
I've seen that in C++ code, the functions are declared before the main, but the functions are defined after main braces are closed. I'm confused by this. If the function is called in main, but the function is not yet defined, how does the program know where to go to find the function in the code to execute it? I mean, does the computer learn where everything is when it compiles the code? Am I making any sense? Any help would be appreciated.

kmj
01-21-2002, 11:11 PM
you'll notice that any function defined after the main will have a prototype somewhere before the main function. This is so that the compiler knows a function is being used correctly (is being passed the proper parameters, etc.). Note that the compiler won't necessarily care if the function is defined at all in the program, because "compiling" actually consists of two phases: compiling, and linking. (this is what allows you to use multiple source code files). If something isn't defined in a "module" (the object code that results from compiling a source file), the compiler may assume (or be specifically told) that a function will be defined in some other module (the 'extern' keyword is used for this), and therefore compile the source file completely without errors. Then, once all the source files are compiled into object files (.o or .obj, depending in platform), the object files are linked together with a linker into either a single executable or a static library or a dynamic link library. Using gcc (which I think is technically a compiler) generally implicitly calls the linker to link the object files once they've been created.

Hope that clears things up a bit.

yawningdog
01-22-2002, 08:15 PM
Allow me to try again.

When I was reading the source code exercise in the function chapter of my book, I saw that halfway through main, I saw the function being called. I then said to myself "How can it do that? You can't pass parameters to a function that doesn't yet exist but in prototype."

But then as I read further, I saw that the function was defined later in the code and I then said to myself "Oh, there it is."

My question is, why doesn't my computer ask the same question, unless it does as I did and read all of the code before trying to run the program? I guess I was expecting something akin to a "goto" instruction.

I do understand that code with a declaration but not a definition of a function will compile and link legally.

Strogian
01-22-2002, 09:09 PM
You want a pretty technical answer, I assume? I've never written a compiler, so I, unfortunately, don't know how it's done. I could guess for you, though. :) When the program is linked, it probably does put a sort of 'goto' instruction in the executable code. If the function is actually in the executable, then it would just say to call the function at offset x in the file. If the function is outside the function (in a shared library, I guess?), then it would have to link to that file. That's just a guess, but it makes sense to me. :)

bwkaz
01-22-2002, 11:09 PM
I'm guessing that the compiler makes 2 passes through the source file. I know some old DOS x86 assemblers did this, so maybe gcc does it too.

If the function is in a shared lib, then I think the compiler just inserts a "call stub" instruction (BTW, call is assembly for "goto, but save the current address so I can return here" -- it's kinda funny that at assembly level, almost every control structure in "structured programming" (programming without gotos) breaks down to a goto). Then when the object code gets linked, the stub ends up calling the right address.

Or, it might just flag the offset into the object file where the undefined reference is, then the linker changes the offset at this address when the program is linked. Actually, come to think of it, this sounds more likely.

Stuka
01-23-2002, 01:40 PM
The compiler does indeed make multiple passes through the source code - more than 2, I think, but I can't recall. This also works for all your included header files - they only contain prototypes, not function definitions. When you include headers, or prototype a function prior to main, the compiler checks to make sure that your use of the function is correct. It's the linker's job to locate the function - that's why you'll often need to use the -l flag in gcc - the linker won't look in your libraries automatically (I think it's just the standard libs it checks). When the linker finds the function's object code, it creates the link between your executable and the library (statically or dynamically as appropriate).

yawningdog
01-23-2002, 07:31 PM
Much clearer now. Thank you all.