Improve company productivity with a Business Account.Sign Up

x
?
Solved

When to create a header file for a C function?

Posted on 2004-04-06
8
Medium Priority
?
393 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 1000 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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 

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
 

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

What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

595 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