0

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.

4
  • 1
    Depends on what your function does. If you do a heavy work in it I mean, a work that consume the CPU for a long time say 10 ms then it is not a good practice to do it. But if your function does some small amount of work that will not consume the CPU a lot e.g. just makes some basic calculations and returns fast there will not be a problem. Commented Dec 4, 2020 at 15:12
  • 1
    Nested functions are not allowed in standard C. GNU C compiler supports this as an GNU extension. FreeRTOS tasks are just C functions with a specific argument list. Commented Dec 4, 2020 at 19:57
  • 1
    @Kozmotronik Have you perhaps misread the question? It is not about whether a function should be called from within a task, but rather whether the function definition should appear nested within the task function. It is a bad idea because it is not valid C, not because of the run-time of the function. That is irrelevant in any event because it is a pre-emptive scheduler, and it may be entirely legitimate for a low-priority task to run for long periods so long as real-time deadlines are met by appropriate priority assignment using RMA principles. Commented Dec 4, 2020 at 20:32
  • @Clifford you are right, I misread the question. Commented Dec 5, 2020 at 21:21

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.