Sign in to follow this  

Very large function...can I make it global?

Recommended Posts

I have a series of very large functions I would like to be able to use throughout my code. My main.cpp file is becoming very long so I would like to move these functions into separate files. How do I go about doing this? Should I use keyword static? Thanks for any help

Share this post

Link to post
Share on other sites

// in foo.cpp

// this is called the function "definition"
void GlobalFunction( int i )
// do stuff

// in foo.h

// this is called the function "declaration";
// other files need to see this declaration to
// be able to call it
void GlobalFunction( int i );

// in main.cpp

#include "foo.h"

int main( )
// call your global function; this is possible
// because you included "foo.h", and hence the
// function declaration is visible at this scope
GlobalFunction( 0 );
return 0;

Share this post

Link to post
Share on other sites
[EDIT: emeyex beat my by two seconds!]

No, don't use 'static' ;)

Simply copy the function prototype (the first line of the function, with the return type, name and arguments) and put it in a header file.

Currently you have:
void foo( int bar )

So create a new header, say "myFunctions.h", and put this in it:
void foo( int bar );

Now you can put your "foo" function in any CPP file that you like. And you can use it in any other CPP file that you like - as long as those other files have "#include "myFunctions.h"

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this