开发者

Function Declaration/Function Definition

Does it matter if the function definition is before the int (main) or a开发者_C百科fter the int main?

I've seen it both ways and am trying to find the proper way to display the function definition and declaration.


No .. it doesn't. Its a matter of preference. Choose which you prefer and be consistent!


The function definition (which contains the actual code) can be anywhere, even in a different file, as long as the declaration (the function prototype) appears before you call the function.


It is basically a matter of preference. The only requirement is that function declarations (not definitions) precede calls to the function.

As a matter of style, I would generally keep the function definition with the function declaration unless there is a reason to separate them. Which implies that all function definitions would come before the main() definition.


The function definition can be before or after main or even in a different file. What is necessary is that a declaration (or 'prototype') of the function is before the code that uses the function.

Where you put your code can have an effect on compile times. If all the code is in one file, it takes longer to recompile a small change, but if you put code in different files, a small change can take less time to recompile into the executable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜