Vin*_*ick 2 c++ function-prototypes
我在CPP中有以下代码.
//My code
#include<iostream>
using namespace std;
int main()
{
int a;
int display();
int printfun(display());// Function prototype
printfun(9);//Function calling
return 0;
}
int printfun(int x)
{
cout<<"Welcome inside the function-"<<x<<endl;
}
int display()
{
cout<<"Welcome inside the Display"<<endl;
return 5;
}
Run Code Online (Sandbox Code Playgroud)
编译时会抛出错误"Line8:'printfun'不能用作函数".
但是当我在显示功能中进行printfun调用时,相同的代码工作正常.
#include<iostream>
using namespace std;
int main()
{
int a;
int display();
int printfun(display());// Function prototype
return 0;
}
int printfun(int x)
{
cout<<"Welcome inside the function-"<<x<<endl;
}
int display()
{
printfun(9); // Function call
cout<<"Welcome inside the Display"<<endl;
return 5;
Run Code Online (Sandbox Code Playgroud)
}
有谁可以解释这背后的原因?
int printfun(display());// Function prototype
Run Code Online (Sandbox Code Playgroud)
那不是功能原型.这是一个变量声明,相当于:
int printfun = display();
Run Code Online (Sandbox Code Playgroud)
函数原型"可以"在main()中完成,但将它们放在源文件的顶部更为正常.
#include <iostream>
using namespace std;
// Function prototypes.
int display();
int printfun(int x);
int main()
{
int a;
printfun(9); // Function call.
return 0;
}
// Function definitions.
int printfun(int x)
{
cout << "Welcome inside the function-" << x << endl;
}
int display()
{
printfun(9); // Function call.
cout << "Welcome inside the Display" << endl;
return 5;
}
Run Code Online (Sandbox Code Playgroud)