What is the precedent for declaring functions of functions within bodies as other functions?

I found this snippet in the FreeRTOS source:

void vApplicationIdleHook( void ) { /* The simple blinky demo does not use the idle hook - the full demo does. */ #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 ) { extern void vFullDemoIdleHook( void ); //* Implemented in main_full.c. */ vFullDemoIdleHook(); } #endif } 

Why do you need to declare functions / methods like this? What is the advantage? I also saw similar Java code.

+6
source share
2 answers

I assume this is the only place in the project that uses vFullDemoIdleHook , so it is clear and concise to just keep the declaration and call function in these few lines of code.

What would be the advantage of ad elsewhere? Consider an alternative ... this is probably what you are more used to seeing:

 /* The simple blinky demo does not use the idle hook - the full demo does. */ #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 ) extern void vFullDemoIdleHook( void ); #endif void vApplicationIdleHook( void ) { /* The simple blinky demo does not use the idle hook - the full demo does. */ #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 ) { //* Implemented in main_full.c. */ vFullDemoIdleHook(); } #endif } 

I do not see the benefits for this

+8
source

I would say that there is no reason to declare a function inside a function, since it gives the false impression that it is somehow limited only to this function, while it is not. Functions have an external connection (except that your code specifically has extern for the vFullDemoIdleHook() function) by default, and declaring internal functions should be considered as bad practice (but really valid).

Prototypes should be in the header file (or at the top of the source file if there is no header). I would move the declaration to main_full.h :

  extern void vFullDemoIdleHook( void ); /* extern keyword is redundant here */ 

main_full.c:

 void vApplicationIdleHook( void ) { /* The simple blinky demo does not use the idle hook - the full demo does. */ #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 ) { //* Implemented in main_full.c. */ vFullDemoIdleHook(); } #endif } 

If you are not going to use the same function name vFullDemoIdleHook for another purpose (which would be horrible), you do not need to conditionally ( #if ) declare function prototypes.

+1
source

Source: https://habr.com/ru/post/977705/


All Articles