Solved

When to create a header file for a C function?

Posted on 2004-04-06
8
336 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:unityxx311
8 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10767209
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
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 250 total points
ID: 10767257
unityxx311,
> definition
should read "implementation".

Some "rules" of good practice are:

* Split functions into files so that you group the functions by similar tasks or purpose.
* Always use prototypes. Put globally used prototypes in a globally used .h file.
* Same with global variables. Declare them as extern in the header file and have the "implementation" in one of the .c files.
* Functions or variable which are not used outside the file that they are declared in, should be declared as "static". Static functions and variables do not pollute the global namespace.


Stefan
0
 
LVL 2

Expert Comment

by:anupvijay
ID: 10767258
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
0
 

Author Comment

by:unityxx311
ID: 10767279
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:unityxx311
ID: 10767328
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.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10767396
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.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10767498
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
0
 

Author Comment

by:unityxx311
ID: 10767894
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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now