Function declaration/definition

My understanding of the use of function declarations/definitions is the follows:

You wouldn't define a function just in your source code as you would have to define it every single time you needed it across multiple files.

So, you declare it in a header file, define it in its own source file (linked(?) to the header somehow?) and then any time you call the function in your source code the compiler looks in the header for the definition.

But why don't you define a function in a header file instead of just declaring it? Is it because if you had multiple source files that used the function at some point, then you'd have to include the header in all of them, which would mean defining the function multiple times?

Please correct me on anything I have wrong, or that is sort of correct but I haven't phrased well.
You wouldn't define a function just in your source code as you would have to define it every single time you needed it across multiple files.


Actually, you would define it in your source code, and you would do it just once; you wouldn't need to define it across multiple files (in fact, if you did define it across multiple files, the linker would refuse to link and your program wouldn't build).

any time you call the function in your source code the compiler looks in the header for the definition.

No, the compiler uses the declaration or the definition (whichever it can see) to check that the function is being used properly. It doesn't care what kind of file it is in; the preprocessor does all the #include stuff and the compiler only sees a single text file, which must have the declaration or the definition before you try to use it. The compiler does not need to see the definition (this is how libraries work - the compiled library contains the actual function code and the compiler never gets to see that library).

But why don't you define a function in a header file instead of just declaring it?

Because if you included that header file in more than one cpp file, the linker would see identical functions across the different object files (the compiler makes one object file per cpp file, and the linker joins them together), wouldn't know which one to use, and would refuse to link and your program wouldn't build.
Last edited on
If you include a header with a function declaration, then call the function at some point in the source code, then everything must be linked to be able to use the function. The source code containing the call must be linked to the header with the declaration which must be linked to the function's own source file.

So if the source file with the call to the function is eventually linked to the actual function's source code, then why do you need to include headers across the rest of the source files if they are all linked to the function's source anyway?
So if the source file with the call to the function is eventually linked to the actual function's source code, then why do you need to include headers across the rest of the source files if they are all linked to the function's source anyway?


The compiler gets to a function call. It has a job to do. It must check that you are passing the correct parameter types to the function, and that you are dealing with the returned type appropriately. To do this, it must know what the correct input parameter types are, and what the correct return type is. If the declaration (or definition) isn't in this source file, how is the compiler supposed to know? The compiler gets to see one file only.

It can know this in one of two ways. Either is has already seen the function definition, or it has seen a function declaration. This is all a function declaration does; tells the compiler what the correct input and output types are.

The source code containing the call must be linked

By the time linking happens, the source code no longer exists. All you have are a set of object code files, which the compiler promised make their function calls with the right number and types of input parameters, and handle the return type correctly. Each function call is joined to the right function in some other object file by the linker; the linker does this because the compiler effectively left a note in the object code, saying "here, please run the function with this name, which takes these inputs, and returns an object of this type." Functions are identified by all these aspects in C++, so the linker needs to be given all these details, so the compiler needed to know all these details.

why do you need to include headers across the rest of the source files

Not the rest. Only in any source file that uses one of the functions declared in the header.
Last edited on
Ah, so the point of declaring the function in a header is because everything associated together by the linker is turned into one 'central' file? And you can't (shouldn't) define a function in a header because if you include that header across multiple, linked files, then the compiler doesn't know which definition to use.

And what exactly does the header do? When I have a function shorterString() and the directive #include "shorterString.h" is the header file actually replaced with the function definition?
Ah, so the point of declaring the function in a header is because everything associated together by the linker is turned into one 'central' file?

Not really. It's so the compiler knows what the functions inputs and outputs are, and a header is just a convenient way of doing it that's easy to #include into a source file. See the answer at the bottom for a better understanding of what #include someHeaderFile.h does. The point of not defining it in the header, though, is so that you don't screw the linker over when time comes to jam all the object files into one single library or executable.

And you can't (shouldn't) define a function in a header because if you include that header across multiple, linked files, then the compiler doesn't know which definition to use.

The linker won't know which one to use.

When I have a function shorterString() and the directive #include "shorterString.h" is the header file actually replaced with the function definition?

Every file you indicate with the #include directive is copied into that exact spot in your code where the #include is, just as if you had cut and paste it there yourself. That's it. That's all it does. So if you #include a header file, it is identical to you having typed the entire contents of the header file into that spot.
Last edited on
But if it's tantamount to cutting and pasting the function definition, and you are including the header over multiple source files that are linked, then aren't you still defining the function more than once?
But if it's tantamount to cutting and pasting

It's not tantamount to it. It IS cutting and pasting the entire header in there. This is why we do not put the definition in the header.

then aren't you still defining the function more than once?

No. We do not put the definition in the header.

The declaration is in the header, then #include drags the declaration into the source so the compiler knows what the function looks like, and in one single source file somewhere is the definition so the linker can join up the function calls to the function.
Last edited on
Okay I've pretty much got it now...thanks for your help (and patience!)
Topic archived. No new replies allowed.