How to make predefined function??

Oct 29, 2008 at 1:45pm
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 Oct 29, 2008 at 1:48pm
Oct 29, 2008 at 3:30pm
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.
Oct 30, 2008 at 4:29am
I think you need to get a book...

here is one for free:

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
Oct 31, 2008 at 9:26am
yes , thats what i need , to make my own header file,can any one explain some more on this?
Oct 31, 2008 at 10:22am
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.
Oct 31, 2008 at 4:02pm
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)?
Nov 4, 2008 at 6:24pm
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 Nov 4, 2008 at 6:26pm
Nov 4, 2008 at 7:24pm
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.