How to make predefined function??

i want to make a predefined function of mine own. plz tell me which header file to modify and how. i am a beginner here so plz be a bit more specific.i use turbo and borland compilers.
Last edited on
What do you mean by "predefined function"?

Do you just mean you want to write your own function?

In that case, you create your own header file with the prototype in it.

Never modify any of the standard headers for any reason.
I think you need to get a book...

here is one for free:

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
yes , thats what i need , to make my own header file,can any one explain some more on this?
You simply create a file with a ".h" extension such as "myFunctions.h" .
In that file you put the function prototype
EG
 
int myAddFunction(int,int);

You then create a second file with a ".cpp" extension ("myFunctions.cpp") and put the body of the function in that
EH
1
2
3
4
5
#include "myFunctions.h"  //Note "" not <> as is your library not system
int myAddFunction(int a,int b)
{
   return a+b;
}

Then in any file you need to call myAddFinction() simply put
 
#include "myFunctions.h" 

at the top with your other #includes.
You can have as many functions or classes as you like declared in the .h file. Note that this should only have functions in it that you want to be available to other blocks of code. You should try to group functions in some logical structure when you do this.
Never modify a standard headerfile!

And i guess you do know how to create you own functions within a simple code (.cpp, without using headerfiles)?
Just out of curiosity, why would you make two files? why not just name the .cpp a .h file and save the extra space? Unless this is compiler specific. I use the VC++ Express compiler, however I use Notepad++ for actual code editing. Anywho point was I would think it would be more efficient to just use one file instead of two.
Last edited on
Code bloat and duplicate symbol problems.

If you implement a function in a file that gets #included directly by, say, 5 other .cpp files, then when the compiler compiles those 5 .cpp files into .o(bj) files, each object file gets a complete copy of the compiled version of that file. Which will lead to two problems -- first, when you go to link all the .objs into an executable, the same symbol will be defined in every .obj file causing linker errors, and second, why have multiple copies of the same compiled code in the application at all (it just makes your application bigger)?
Topic archived. No new replies allowed.