[英]Calling a function in C++
I'm new to the whole C++ programming thing, and I know this is a easy solution, but I just cant figure it out! 我是整个C ++编程人员的新手,我知道这是一个简单的解决方案,但我无法弄清楚! I simply just wanna call a function that prints out 1 + 4. Here's the code:
我只是想调用打印出1 + 4的函数。这是代码:
#include <iostream>
using namespace std;
int func()
{
cout << 1 + 4;
return 0;
}
int main()
{
int func();
}
It shows nothing in the console window, only that the application stopped with return code 0. Can someone tell me what's wrong? 它在控制台窗口中什么也没有显示,只是应用程序停止并返回代码0。有人可以告诉我这是什么问题吗?
You are not calling func()
function correctly: 您没有正确调用
func()
函数:
int main()
{
// int func(); This line tries to declare a function which return int type.
// it doesn't call func()
func(); // this line calls func() function, it will ouput 5
return 0;
}
you can just call the function by its name. 您只需按函数名称调用即可。 Like func();
就像func();
int func()
{
cout << 1 + 4;
return 0;
}
the above function is retruning an integer. 上面的函数是整数。 you are returning 0. to make it more useful return the sum and catch it in main function.
你返回0.使它更有用返回总和并在主函数中捕获它。
int func(){
return 1+4;// return 5 to main function.
}
now in main. 现在主要。
int main (){
int ans = func();// ans will catch the result which is return by the func();
cout<<ans;
return 0;
}
try to understand the working of each statement. 试着理解每个陈述的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.