I'm writing an Arduino program, and I need to write a function to input some numbers trough a computer connected to it trough the serial interface.

This function should perform many different and well-defined tasks, e.g.: obtain data (characters) from the computer, check if the characters are valid, transform the characters in actual numbers, and many others. Written as a single function it would be at least 500 lines long. So I'll write a group of shorter functions and one "main" function that calls the others in the only meaningful order. Those functions will never be called in the rest of the code (except of course the main function). One last thing - the functions need to pass each other quite a lot of variables.

What is the best way to organize those functions? My first tough was to create a class with only the "main" function in the public section and the other functions and the variables shared by different functions as private members, but I was wondering if this is good practice: I think it doesn't respect the C++ concept of "class"... for example to use this "group of functions" I would need to do something like that:

class GetNumbers {
        //using the constructor as what I called "main" function
        GetNumbers(int arg1, char arg2) {
        performFirstAction() {...};
        performSecondAction() {...};

        bool aSharedVariable;
        int anotherVariable;

And where I actually need to input those numbers from the computer:

GetNumbers uselessName (x,y);

Making the "main" function a normal class method (and not the constructor) seems to be even worse:

GetNumbers myClass;
//somewhere else in the same scope
  • And what is wrong with just using one big function? It sounds like, in this case, splitting it into multiple functions won't make it easier to read/understand. Just add plenty of comments, and add some extra newline between different sections.
    – Gerben
    Feb 15, 2018 at 16:03

1 Answer 1


Maybe it's my background of old C programmer but... I would just write plain functions, no classes, like in C. And I would keep all this stuff in a single file, to provide some form of encapsulation:


some_type get_numbers(int arg1, char arg2);


static int performFirstAction(float arg1, int arg2, char arg3)

static bool performSecondAction(long arg1, void *arg2, int arg3)

some_type get_numbers(int arg1, char arg2)

Note that there is only one function declared in the .h: this is the “interface” to your module. Note also that all the other functions are defined as static. This tells the compiler to keep them hidden from the linking phase, so they do not pollute the global namespace. And finally, note that there is no global shared variable: everything is passed as arguments to the functions.

The rationale for the last point is that this helps the compiler optimize things. A static function that is called only once is almost guaranteed to be inlined by the compiler. Which means there is no overhead in passing the arguments. On the other hand, if you use global variables and the compiler doesn't manage to optimize them away, you will incur the cost of RAM access.

  • 1
    I second that. Classes are for things that have data+methods. For a bunch of unrelated random functions, old style C functions are good enough. Try written a callback using C++ class method ...
    – user31481
    Feb 15, 2018 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.