How to separate functions and main file?

Hi everyone!
I'm making a little project that suddently grow up :)
I have 42 functions, many of them call other functions, so it's becoming a mess to scroll the entire code to add or modify them.

I want to put all these functions in other files, sorted by purpose, and then include them in the main code with some '#include' thing, maybe headers?

Here's a little example of two functions that i want to separe from main file and "dislocate" in two different "files" (one functions calls the other one):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//MOVE DOWN: CURSOR AND MAP NAVIGATION
void mooveDown (int &navigateRow, int &rp, char mode, int extremeRowPointer, int startMapR, int startMapC, int nrig, int ncol, int cp, int rigMapResol, int colMapResol, int navigateCol, char C[], char F[], char B[])
{
    if (mode=='N') {
        if (rigMapResol+navigateRow < nrig){
        //moove map
        navigateRow++;
        //update view
        printMap (startMapR, startMapC, nrig, ncol, rigMapResol, colMapResol, navigateRow, navigateCol, C, F, B);
        }
    }
    else if(mode=='n'){
        if (rigMapResol+navigateRow < nrig){
        //moove map
        navigateRow=navigateRow+rigMapResol;
        if (rigMapResol+navigateRow > nrig) {navigateRow = nrig-rigMapResol;}
        //update view
        printMap (startMapR, startMapC, nrig, ncol, rigMapResol, colMapResol, navigateRow, navigateCol, C, F, B);
        }
    }
    else {if (rp != startMapR+extremeRowPointer-1){rp=rp+1; gotoyx(rp,cp);} }
}




//PRINT MAP
void printMap (int startMapR, int startMapC, int nrig, int ncol, int rigMapResol, int colMapResol, int navigateRow, int navigateCol, char C[], char F[], char B[])
{
    if (rigMapResol > nrig) { rigMapResol = nrig;} //non modifichi MapResol! not reference&
    if (colMapResol > ncol) { colMapResol = ncol;} //non modifichi MapResol! not reference&
    for (int ir = startMapR ; ir < startMapR+rigMapResol; ir++){
        for (int ic = startMapC; ic < startMapC+colMapResol; ic++){
            gotoyx(ir,ic);
            SetConsoleTextAttribute(HC, translateColorChar(F[(ir-startMapR+navigateRow)*ncol+(ic-startMapC+navigateCol)] , B[(ir-startMapR+navigateRow)*ncol+ (ic-startMapC+navigateCol)]) );
            cout << C[((ir-startMapR+navigateRow)*ncol) + (ic-startMapC+navigateCol)];
        }
    }
}


And a lot of my functions uses these two:
1
2
3
4
5
6
7
8
9
10
11
12
//GO TO (Y,X)
void gotoyx (int r, int c)
{
    COORD coord; // coordinates
    coord.X = c; coord.Y = r; // X=riga and Y=colonna coordinates
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); // moves to the coordinates
}

//COLORS
SetConsoleTextAttribute(HC, color);
HANDLE  HC = GetStdHandle(STD_OUTPUT_HANDLE);


I'm using Code::Blocks 17 and C++, the code is 1300 lines long so it's complicate to make you see. I'm a beginner so i don't know much about classes and other C++ capabilities.

I searched a lot online but i'm not an english-speaker so it's hard to understand well (that's why i prefer something graphic, video).

Thank you :)!
Last edited on
Without seeing your code, it's hard to make suggestions.
Is this C code or C++ code?

If C++ code, then you should already be grouping your functions by class with a .h and .cpp file for each class.

If C code, then you want to group related functions together.
You can create .h and .c files for C code just as you would for C++ code.
Perhaps your functions fall into logical categories such as utility functions, graphic functions, calculation functions, etc.

Even in C, it's not uncommon to have objects as you would in a C++ program.
This makes for a logical grouping of functions to support each object type.
Open post updated!
Let's take the first example. You need to put a declaration of the two functions in a header file:
file.h:
1
2
3
void mooveDown (int &navigateRow, int &rp, char mode, int extremeRowPointer, int startMapR, int startMapC, int nrig, int ncol, int cp, int rigMapResol, int colMapResol, int navigateCol, char C[], char F[], char B[]);

void printMap (int startMapR, int startMapC, int nrig, int ncol, int rigMapResol, int colMapResol, int navigateRow, int navigateCol, char C[], char F[], char B[]);


By the way, that's a lot of parameters, which is a pretty good hint that you need to create
some classes or something else to combine the data.

Then you put the files in some .cpp file and include the header file:

file.cpp:
1
2
#include "file.h"
// Add the code for the functions. 


Finally, add #include "file.h" to your main.cpp. Now you can call the functions from within main.

You say that you have 42 functions and about 1300 lines of code. I wouldn't put each function in its own file. Group them by functionality into 5-10 groups.
Also think about removing/combining some of that duplicate code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//PRINT MAP
void printMap (int startMapR, int startMapC, int nrig, int ncol, int rigMapResol, int colMapResol, int navigateRow, int navigateCol, char C[], char F[], char B[])
{
    if (rigMapResol > nrig) 
        { rigMapResol = nrig;} //non modifichi MapResol! not reference&
    if (colMapResol > ncol) 
        { colMapResol = ncol;} //non modifichi MapResol! not reference&
    for (int ir = startMapR ; ir < startMapR+rigMapResol; ir++){
        for (int ic = startMapC; ic < startMapC+colMapResol; ic++){
            gotoyx(ir,ic);
            size_t index = (ir-startMapR+navigateRow)*ncol+(ic-startMapC+navigateCol);
            ///// Make sure you validate "index" to insure you don't overflow/underflow your array in case of bad data. No sense of doing the calculation multiple times, error prone.
            SetConsoleTextAttribute(HC, translateColorChar(F[index] , B[index]) );
            cout << C[index];
        }
    }
}

dhayden thanks, i'm practing for one or two months so i yet don't know how classes work, it will takes some more weeks (i'm following a youtube course of a professor).

Thank you jlb, i don't know 'size_t', i'm a beginner, so i prefer my rough code.
i don't know 'size_t', i'm a beginner, so i prefer my rough code.

So then use an int, unsigned int or a long or unsigned long, but you really really should check that calculation to insure it doesn't underflow or overflow the bounds of the array. Having complicated calculations controlling the access to an array is a very bad practice. Also there is no need to do that calculations several times, just do it once.

Topic archived. No new replies allowed.