Using external files for your functions

 

This tutorial will show you how to create a project that uses an external file to hold commonly used functions. This allows you to keep your core source code file (the one that contains the main function) as minimal as possible and to sort functions that do similar things into their own files.

 

 

Click here to jump to the top of the page1 : Create the project

Create a new empty project in the development environment of your choice. For the examples shown here I will be using Visual Studio.

Click here to jump to the top of the page2 : Create the function header file

Add header fileFirst, you need to create the external functions' header file. This will be the file that the function headers will be placed in. This is the file that will be included in your main source code file.

Right-click on the Header files section of the Solution Explorer and select Add then New Item.....

The Add New Item window will open and display all of the possible resources that can be added. Make sure that Code is selected in the Categories list on the left and then Header File (.h) in the panel on the right.

Call the file functions.h and click on the Add button.

Add the new header file

Click here to jump to the top of the page3 : Create the functions source file

Right-click on "Souce Files" in the Solution ExplorerYou now need to create the source code file for the functions. This will be the file that the source code for your function bodies will be written in. Its name must match that of the header file so that when the header file is included in a souce code file, the function body code can be found in this file.

Right-click on Source Files in the Solution Explorer and select Add then New Item...to display the Add New Item window.

C++ File (.cpp)Again, make sure that Code is selected in the list on the left.

In the panel on the right, select C++ File (.cpp).

For the name you must use the same name as the header file but with a ".cpp" extension instead of ".h".

I called the header file, "functions.h" so I must call the source file, "functions.cpp".

Click the Add button to add the new source code file to the project.

Click here to jump to the top of the page4 : Create the main source code file

Follow the steps for step 3 above except this time call the file main.cpp.

The file you are currently editing is shown in bold on the tabs at the top of the code editorMain.cpp should now be open for editing in the code editor. You can confirm this by looking at the tabs along the top of the editor. The source code file in bold is the one that you are currently editing.

If main.cpp is not currently shown in bold on the tabs, click on the main.cpp tab. You're going to need to put code in the main source code file in the next step.

 

Click here to jump to the top of the page5 : Add the code for main.cpp.

In the code editor, enter the following code for main.cpp. The comments in the code explain what each line of code does.

main.cpp

// Include the input/output stream library
#include <iostream>

// Include my own functions library
#include "functions.h"

// Use the standard namespace
using namespace std;

// Start the main function
int main(void){

    // Using the power function from my functions library,
    // display 2 to the power of 8 (which should be 256)  
 

    cout << "2 to the power of 8 is " << power(2,8) << endl;

    // Wait for the user to press a key before ending
    system("PAUSE");

    // Return a success value from the main function
    return EXIT_SUCCESS;
}

Click here to jump to the top of the page6 : Add the code for functions.cpp

Click on the the tab with "functions.cpp" so that it is marked in bold.On the tabs at the top of the code editor, select functions.cpp.

In the code editor, enter the following code:

functions.cpp

// Function to calculate an exponential number
// E.g. 2 to the power of 6 = 2x2x2x2x2x2 = 64

int power(int base, int power){
   
    // Create an integer variable called total, initialised to 1
    int total = 1;

    // Use a for loop to loop through the powers,
    // multiplying the running total by the base with each iteration

    for (int loop = 1; loop <= power; loop++){
        total = total * base;
    }

    // Return the contents of total
    return total;
}

Click here to jump to the top of the page7 : Add the function header to functions.h

Right now, main.cpp has no way of knowing that the power function even exists in functions.cpp. To link functions.cpp to main.cpp you must add the function header for power to functions.h.

Click on "functions.h" on the tabs at the top of the code editorOn the tabs at the top of the code editor, select functions.h.

In the code editor enter the following code:

functions.h

// Header for the power function
int power(int base, int power);

Click here to jump to the top of the page8 : Build and test the program

The completed program outputThat's all the code done. All you need to do now is compile and run the program.

If you're using Visual Studio, press F5 to compile and run your program.

If you're using Dev-C++ press F9 to compile and run your program.

Click here to jump to the top of the page9 : Going further (using more functions)

It's very doubtful that your future programs will only ever use one function but expanding beyond a single function is easy - just remember to put your other function bodies in functions.cpp and the function header for each function in functions.h.

The function header is basically the first line of the function but instead of an opening curly brace, it ends in a semi-colon.

For example:

First line of my function: int power(int base, int power){
Function header:    int power(int base, int power);