I would like to clarify if I can specify, both the function prototype and the function definition in a header file?
Is it necessary for me to create a source file to define the functions which are declared in a header file?
Thank you...
I would like to clarify if I can specify, both the function prototype and the function definition in a header file?
Is it necessary for me to create a source file to define the functions which are declared in a header file?
Thank you...
Is it necessary for me to create a source file to define the functions which are declared in a header file?
It is not necessary, just recommended.
If you have a function defined in a header file, it will be defined in every translation unit (source file) that includes that header.
Unless you take steps to avoid it, this will result in errors when you link the various translation units together ("multiple definition...").
One way to avoid this would be declaring the function inline
.
Generally speaking, though, you are better off with declarations in the header and definitions in a source file (translation unit), both for compilation speed and maintainability.
Until you get to the subject of template functions (which pretty much have to be defined in headers). But they are a rather different beast altogether, and I will not confuse you with them at this point. ;-)
Yes you can define all of your function in the header file. This is done in header only libraries.
Generally one does not do this as it can lead to code bloat and longer compilation times but it is not required.