• Advertisement

Archived

This topic is now archived and is closed to further replies.

Question on functions...

This topic is 5810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to store functions in a cpp file, then use them in the rest of the program? If so, could someone give an example of how to call the function.

Share this post


Link to post
Share on other sites
Advertisement
this is what header files are for. say you have a function SomeFunc() which you implement in a .cpp file:
// func.cpp
int SomeFunc(int Arg1)
{
return (Arg1 + Arg1);
};

you make a header file for this, and just include the function declaration:
// func.h
int SomeFunc(int);

then, in any other source (.cpp) file you want to use that function in, just include the header at the top:
// main.cpp
#include < func.h>

int main(int argc, char* argv[])
{
return SomeFunc(5);
};

HTH.

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
Don''t forget to add the ifdef/define in the header file so that you won''t get redefinition erros.

// func.h
#ifndef FUNC_H
#define FUNC_H

int SomeFunc(int);

#endif

Share this post


Link to post
Share on other sites

  • Advertisement