Why use function prototypes?

前端 未结 3 1888
慢半拍i
慢半拍i 2021-02-07 13:48

Why use function prototypes in C? It seems sort of redundant because we already declare the function name, argument types, and return type in the definition. Do the prototypes h

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 14:22

    Generally speaking, you don't need to explicitly declare functions because defining them also declares them. Here are two situations where you would need to:

    1. The definition of the function is in an external module.

      For example, if the function is defined in foo.c, but you want to call it from bar.c, you will need to declare the function in bar.c or a file included by it (typically, foo.h).

    2. The definition of the function comes after a call to it.

      For example, if you have two functions that call each other, you will need to declare the second one before the definition of the first one.

提交回复
热议问题