Warm tip: This article is reproduced from serverfault.com, please click

Is it a good habit to implement functions inside FreeRTOS Tasks?

发布于 2020-12-04 13:42:09

Im new to FreeRTOS and Real Time Operating Systems. I was wondering if it is a good programming style to implement functions inside FreeRTOS Tasks. for Example

void displayTask(void* param) {

    static void Factorial (params) {
        // doSth
   }

   for(;;) {

       //call it here
       Factorial(params);
   }

}

or is it better to just implement it outside the Task?, because in Case i have a lot of functions it would be difficult to read the code i guess.

Questioner
unkown53
Viewed
0
Clifford 2020-12-05 04:33:57

Nested functions are not valid C. Your compiler may support it, but it is ill-advised in most cases to write code that can only be compiled by specific compilers. This is especially true in embedded systems where GCC is not be supported for many targets you might wish to use.

The only benefit of nesting a function in this way is to restrict its scope so that it is callable only within the enclosing function.

Using the feature within a task specifically is largely irrelevant, a nested function has no run-time impact, it is compiled, called and runs just like a regular function, the only difference is that at compile time with respect to the visibility of the function from other code.

So it is just "bad practice" in any function, not specifically a task.