I came across this piece in 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 would one declare functions/methods like this? What is the advantage? I have seen similar code in Java as well.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function. The C standard library provides numerous built-in functions that your program can call.
The reason modern compilers give warnings on an attempt to call a function before seeing a declaration is that a declaration allows the compiler to check if arguments are of the expected type.
Declaring a function - function prototypes In C and C++, functions must be declared before the are used. You can declare a function by providing its return value, name, and the types for its arguments. The names of the arguments are optional. A function definition counts as a function declaration.
It is not necessary to declare a function before defining it. But ,it is prefered to declare a function before using it.
I am assuming that this is the only place in the project where vFullDemoIdleHook
is used, so it's clear & concise to just keep the declaration and function call all in these few lines of code.
What would be the advantage of putting the declaration elsewhere? Consider the alternative... this is probably what you're 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 see no advantage to this
I'd say that there's no reason to declare functions inside a function as it gives the false impression that it's somehow limited to that function alone while it isn't.
Functions have external linkage (besides your code specifically has extern
for function vFullDemoIdleHook()
) by default and declaring inside functions should be considered as a bad practice (but valid though).
Prototypes should be in a header file (or at the top of the source file if there's no header). I'd 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
}
Unless you intend to use the same function name vFullDemoIdleHook
for a different purpose (which would be terrible), you don't need to conditionally (#if
) declare function prototypes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With