Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use case of declaring functions methods within bodies other functions?

Tags:

c++

c

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.

like image 590
Atilla Filiz Avatar asked Nov 04 '14 10:11

Atilla Filiz


People also ask

What is declaration and definition of a function when is declaration of a function necessary and when is it optional?

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.

Why function declaration is placed prior to function definition?

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.

How do we declare functions?

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.

Is function declaration necessary in C?

It is not necessary to declare a function before defining it. But ,it is prefered to declare a function before using it.


2 Answers

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

like image 163
tenfour Avatar answered Oct 30 '22 11:10

tenfour


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.

like image 20
P.P Avatar answered Oct 30 '22 11:10

P.P