C function declaration within another function

后端 未结 5 1149
孤城傲影
孤城傲影 2021-01-05 10:49

can anyone explain these lines to me:

int xyz( void )  
{ 
extern void abc( void );
}

a function declaration within a function definition?

5条回答
  •  执念已碎
    2021-01-05 11:48

    This way of declaration has one big advantage:

    If only one or less functions are calling an external function, this declaration makes sense especially within a big source file. If a later code restructuring (function move in another file) has to be done, it is much easier to see the dependencies than to add externals on global (file) scope. In the latter case the probability of "forgetting" such externals in a file is higher. In contrast by declaring it in function scope, the declaration will move together with the function...

    I also tend to do so for external global variables - the benefit comes later when maintaining and eventually restructuring / minimizing dependencies.

    One last note to the topic "writing external / not external": If its just a forward declaration (-> the function is defined at end of the the same file), I would not recommend using external - because it simply isn't the case. Otherwise external makes absolute sense to indicate that definition has to be found somewhere else (or for libaries: might need to be implemented by users of that libary).

    Hope this helps (as a step to a more objective oriented programming style.. :) )

提交回复
热议问题