在C/C++中,显式原型化局部函数是否比在使用前定义函数具有优势? 这里的局部函数指仅在其源文件中使用的函数。 例如:
#include "header.h"
static float times2(float x){
return 2*x;
}
static float times6(float x){
return times2(3*x);
}
int main(void){
// Other stuff ...
float y = times6(1);
// Other stuff ...
}
与之相比:
#include "header.h"
// Local function prototypes
static float times2(float);
static float times6(float);
// Main
int main(void){
// Other stuff ...
float y = times6(1);
// Other stuff ...
}
// Local functions definition
static float times2(float x){
return 2*x;
}
static float times6(float x){
return times2(3*x);
}
个人而言,我更喜欢使用第一种选项,因为需要编写的代码较少,文件也更易读。但现在我想知道是否有任何技术原因偏好第二种选项。
编辑:我已将static添加到times2()和times6()中,请参见@Gangadhar的答案和评论。