C++ 在 main 中调用函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20916162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:23:45  来源:igfitidea点击:

Calling a function in main

c++function

提问by user3150381

I'm just learning C++and I have a little code here:

我只是在学习C++,这里有一些代码:

using namespace std;

int main()
{
    cout<<"This program will calculate the weight of any mass on the moon\n";

    double moon_g();

}

double moon_g (double a, double b)
{
    cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
    cin>>a;
    b=(17*9.8)/100;
    double mg=a*b;
    return mg;
}

It compiles, but when I run it it only prints out:

它可以编译,但是当我运行它时,它只会打印出:

This program will calculate the weight of any mass on the moon

This program will calculate the weight of any mass on the moon

but doesn't execute the moon_gfunction.

但不执行该moon_g功能。

回答by Beta

This line:

这一行:

double moon_g();

doesn't actually doanything, it just states that a function double moon_g()exists. What you want is something like this:

实际上并没有任何事情,它只是说明一个函数double moon_g()存在。你想要的是这样的:

double weight = moon_g();
cout << "Weight is " << weight << endl;

This won't work yet, because you don't have a function double moon_g(), what you have is a function double moon_g(double a, double b). But those arguments aren't really used for anything (well, they are, but there's no reason to have them passed in as arguments). So eliminate them from your function like so:

这还行不通,因为您没有 function double moon_g(),您拥有的是 function double moon_g(double a, double b)。但是这些参数并没有真正用于任何事情(好吧,它们是,但没有理由将它们作为参数传入)。因此,将它们从您的函数中消除,如下所示:

double moon_g()
{
  cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
  double a;
  cin>>a;
  double b=(17*9.8)/100;
  double mg=a*b;
  return mg;
}

(And declare the function before you call it.) More refinements are possible, but that'll be enough for now.

(并在调用之前声明该函数。)还可以进行更多改进,但现在就足够了。

回答by Shafik Yaghmour

This is a function declaration:

这是一个函数声明:

double moon_g();

this won't call a function, and if you did have it correct, which means adding two parameters since that is how you define it below:

这不会调用函数,如果你确实有它正确,这意味着添加两个参数,因为这是你在下面定义它的方式:

moon_g( a, b ) ;

it would not work because you either need to move the definition of moon_gbefore mainor add a forward declaration before mainlike this:

它不起作用,因为您需要移动moon_gbefore的定义main或在main此之前添加前向声明:

double moon_g (double a, double b) ;

Although it seems like aand bare not inputs but values you want to return back to mainthen you would need to use references and it would need to be declared and defined like this:

虽然好像ab没有投入,但要返回到值main,那么你就需要使用引用,它会需要声明和定义如下:

double moon_g (double &a, double &b) ;
                      ^          ^

A useful thread to read especially if you are starting out would be What is the difference between a definition and a declaration?.

一个有用的线程,特别是如果你刚开始阅读,定义和声明之间有什么区别?.

Which compiler you use makes a difference here clangprovides the following warning:

您使用的编译器在这里有所不同会clang提供以下警告:

warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
    double moon_g();
                 ^~

while I can not get gccnor Visual Studioto warn me about this. It is useful in the long run to try code in different C++compilers when you can, it can be a very educational experience and you don't have to install them either since there are plenty of online C++ compilers available online.

虽然我不能得到gcc也不能Visual Studio警告我这一点。从长远来看,尽可能在不同的C++编译器中尝试代码很有用,这可能是一次非常有教育意义的体验,而且您也不必安装它们,因为网上有很多在线 C++ 编译器可用

回答by PTwr

There is huge difference between calling a function and declaring it just as there is difference between local variables and function arguments.

调用函数和声明函数之间存在巨大差异,就像局部变量和函数参数之间存在差异一样。

I suggest reading basic tutorials first.

我建议先阅读基础教程。

Anyway, thats how code should look like:

无论如何,这就是代码的样子:

#include <iostream>
using namespace std;

double moon_g ()
{
    double a,b;
    cout<<"Enter the mass in kilograms. Use decimal point for any number entered\n";
    cin>>a;
    b=(17*9.8)/100;
    double mg=a*b;
    return mg;
}

int main()
{
    cout<<"This program will calculate the weight of any mass on the moon\n";

    cout<<"Result is: "<<moon_g();
}

回答by Jeff

There are two problems in your code.

您的代码中有两个问题。

Firstly, if you want to call your function

首先,如果你想调用你的函数

double moon_g (double a, double b) // this means if you want to call moon_g() you must provide arguments a and b, otherwise, the you will encounter an compile error.
{
    cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
    cin>>a;
    b=(17*9.8)/100;
    double mg=a*b;
    return mg;
}

you should provide the two parameters aand b. But aand bare calculated in the body of function definition, it is unnecessary to declare the two parameters. You can write like this.

您应该提供两个参数ab。但是ab是在函数定义体中计算的,不需要声明这两个参数。你可以这样写。

double moon_g () //this means function moon_g() does not accept any arguments
{
    double a, b; // declare a and b in the definition body instead of in the arguments list
    cout<<"Enter the mass in kilograms. Use decimal point for any number entered";
    cin>>a;
    b=(17*9.8)/100;
    double mg=a*b;
    return mg;
}

Then, in the main function, your calling function statement is wrong. You may want to receive the return value. So, you should write the code like this.

那么,在主函数中,你的调用函数语句是错误的。您可能希望接收返回值。所以,你应该像这样写代码。

int main()
{
    cout<<"This program will calculate the weight of any mass on the moon\n";

    double ret = moon_g();
}

Finally, it is mostly recommended that the function which will be called by another function should be declared or defined previously.

最后,主要建议将被另一个函数调用的函数应该事先声明或定义。