Link to home
Start Free TrialLog in
Avatar of unityxx311
unityxx311

asked on

When to create a header file for a C function?

Hi, I am writing a simulation that has gotten rather lengthy for just one file, 600+ lines. When do you think it is appropiate to take functions out of the main file and include them in headers?


thanx, m.
Avatar of stefan73
stefan73
Flag of Germany image

Hi unityxx311,
> When do you think it is appropiate to take functions out of the main
> file and include them in headers?

Never. You'll create duplicate routines when you include the definition.

You probably mean to split them into separate source files with function prototypes in the header file, such as:

main.c:
#include "protos.h"
main(){
    foo("Hello, World!\n");
}
foo.c:
void foo(char* x){
    puts(x);
}

protos.h:
void foo(char* x);


Cheers,
Stefan
ASKER CERTIFIED SOLUTION
Avatar of stefan73
stefan73
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Unity,
Stefan is right, you never actually define a function in a header file.
But yes, you often declare it there.

And it is always a good practise to break down a large source file into smaller source files. with a single header or individual
header, which ever way you see it suitable.

Let us know what aer you exactly looking at.....ok?
Cheers,
Anup
Avatar of unityxx311
unityxx311

ASKER

Stefan,
 Yes thank you for clearing that up, but when is it a good idea to put functions in seperate source files? When do you do it?


thx, m.
Stefan,
  Last comment was posted before reading second post by you. What do you mean by prototypes? Function definitions? As for a little of what I'm doing.. computing an FFT on non-radix 2 data set.. anyone know why gcc 3.3 does not come with a complex package? Just curious because I went and got the gnu sci lib and did not understand why its just not included.

thx, m.
Only necesarry when u tranfer ur code into .lib file to hide the code from the users.

Then,u can use the header file to check the prototype of the fn. called with that declared and if correct call the library to execute the function.
unityxx311,
> anyone know why gcc 3.3 does not come with a complex package?
Complex are a builtin gcc extension. See
http://gcc.gnu.org/onlinedocs/gcc-3.3.3/gcc/Complex.html#Complex

> when is it a good idea to put functions in seperate source files? When do you do it?

To increase maintainability. Above a certain source file size, searching and compiling becomes a pain. With makefiles, you can also split the compilation, so you need only to compile a fraction of all the files if you do a small change somewhere.

A file above ~10000 lines becomes unusable. But modern IDEs have gone a long way to help development with large files.

It's not really a question of just .lib files. It's also a question of decoupling components, avoiding huge monolithic programs and of course it's a question of encapsulation (of subroutines!).

Stefan
Thank you, you guys are great! Just a question, is there any community board out there where you guys talk about diff topics? I havent been able to find a board where the topics are open and with pple who share the same interests as me.. science/engin/programm/computers/etc.
thx, m.