More Code Organisation with functions!

Ive been trying to get my code organized through various ways and I've found across various programs that I use the same functions that I have created, over and over again.

Of course copying and pasting code is not recommended ( and I still do it, I'm trying to avoid it >_<! ), So is there a way to organize my functions into .c files and #include them like any normal headers.

For example, I want to do this
 
#include <printFunctions> 


NOT

 
#include "C:\Users\BlahBlah\printFunctions.c" 


I dont want to be typing that out all the time!
Im using CodeBlocks at the moment!

One last question, what happens if I define a function and dont use it?
Im asking this because im not going to be using all the functions in the .c files

Many Thanks!
Super_Stinger

Other program (Function by itself)

1
2
3
4
5
6
#include "blahblah"

int function(char blah)
{
     //More blah
}


Make a header.

main.cpp

1
2
3
4
5
6
7
8
9
10
#include "blahblah"

int main()
{
       char blah;
       cout << "Blah, now do blah: " << endl;
       //now calling function
       function(blah);
       return 0;
}
In codeblocks, Go to new file, new header, put function in middle

"blahblah" will be the name of the header and what you include
hmm Ive tried multiple things...Thanks for your posts though

What Im trying to do is make a bunch of header files to store my functions so that I can use them across multiple programs, HOWEVER, I want to also assign a name to the header files so i can say #include <test> ( or just "test.h" ) rather then having to give an entire file path.

I am using codeblocks

thanks again
@SuperStinger

I haven't used codeblocks, but you should be able to specify extra include directories that exist in your project source tree.

Be careful not to include .c or .cpp files - include only .h files.

Are you writing C code? If so have a read of this:

http://zanasi.chem.unisa.it/download/C.pdf


It has a chapter which shows how to do header files in C (page 70). The declarations of variables & functions go in a .h file, while the code for those functions goes in a .c file.

If you are doing C++, the convention is to put class declarations in the .h file with definitions of the functions in a .cpp file.

In C & C++, one just creates a mess if one include files with non declarative code everywhere.

HTH

Bahah! A great response TheIdeasMan, I like how youve lead me to the original document by Dennis Ritchie himself! This is very helpful and I think it will help me format my code in other ways aswell, your help is much appreciated

Many Thanks, Super_Stinger!
Last edited on
Topic archived. No new replies allowed.