Prototyping a function - C++

天涯浪子 提交于 2019-12-10 11:58:31

问题


Lately, some of my CPP tutorials have used function prototypes . I understand you must initialize the function, but what is the overall use of it? Couldn't you use just as well write the entire function before main() instead of defining a prototype?

int returnValue(void);

int main()
{
  std::cout << returnValue() << std::endl;
  return 0;
}

int returnValue(void)
{
  return 10;
}

回答1:


One important usage case is when you separate your implementation from the declarations. In other words, you declare your functions/classes etc in a header file, and define (i.e. implement) them in cpp files. In this way, you can distribute your program with the implementation fully compiled in a shared or static library. In order to use a pre-compiled function you need to introduce it to your program via a declaration. Example:

a.h

void f();

a.cpp

void f(){/* implementation here */}

main.cpp

#include "a.h"

int main()
{
    f();
}

Including "a.h" in main() is declaring the function. Once you compile the a.cpp once, you don't need it's source any more, the program will run provided you have at least access to the object file, but in order for the linker to find the function f() you need to declare it.




回答2:


Couldn't you use just as well write the entire function before main() instead of defining a prototype?

I can think of the following cases where you cannot.

Functions used in multiple files

If a function is used in multiple source (.cpp) files, you can define the function only in one source file. You have to declare it in the remaining source files. For convenience and to avoid errors, such declarations are put in header files. The header files are then #included by the source files.

Mutually recursive functions

If foo calls bar and bar calls foo, you cannot implement them without providing a declaration of at least one of the functions.

As a matter of good practice, it's better to declare both functions first. Then you can implement them in any order.




回答3:


If one doesn’t specify the function prototype, the behavior is specific to C standard (either C90 or C99) that the compilers implement. Up to C90 standard, C compilers assumed the return type of the omitted function prototype as int. And this assumption at compiler side may lead to unspecified program behavior.

Later C99 standard specified that compilers can no longer assume return type as int. Therefore, C99 became more restrict in type checking of function prototype. But to make C99 standard backward compatible, in practice, compilers throw the warning saying that the return type is assumed as int. But they go ahead with compilation. Thus, it becomes the responsibility of programmers to make sure that the assumed function prototype and the actual function type matches.

To avoid all this implementation specifics of C standards, it is best to have function prototype.



来源:https://stackoverflow.com/questions/33095087/prototyping-a-function-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!