Search code examples
c++syntaxuser-defined-functionsfunction-prototypes

Syntax for Function Prototyping in Header Files


Consider I have the following function prototype:

void MyFunction(int MyParameter);

With the following definition:

void MyFunction(int MyParameter)
{
    // Do stuff here.
}

Where should they each be put if I have a header file (no main function) with a namespace? Does the prototype go in the namespace and the definition outside it? Or do they both go in?


Solution

  • If you choose to have a namespace, both should be inside :

    .h :

    namespace MyNameSpace {
    void MyFunction(int MyParameter);
    }
    

    .cpp :

    void MyNameSpace::MyFunction(int MyParameter)
    {
        // Do stuff here.
    }