在main中调用函数

时间:2014-01-04 02:37:11

标签: c++ function

我只是在学习 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;
}

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

  

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

但不执行moon_g功能。

4 个答案:

答案 0 :(得分:5)

这一行:

double moon_g();

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

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

这还不行,因为你没有函数double moon_g(),你所拥有的是一个函数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;
}

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

答案 1 :(得分:2)

这是一个函数声明:

double moon_g();

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

moon_g( a, b ) ;

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

double moon_g (double a, double b) ;

虽然看起来ab不是输入而是要返回main的值,但是您需要使用引用,它需要声明和定义像这样:

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

一个有用的线索,特别是如果您开始时将是What is the difference between a definition and a declaration?

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

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

虽然我无法让gccVisual Studio警告我这件事。从长远来看,在不同的 C ++ 编译器中尝试代码是很有用的,它可以是一种非常有教育意义的体验,并且您不需要安装它们,因为有很多{{3 }}

答案 2 :(得分:1)

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

我建议先阅读基础教程。

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

#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();
}

答案 3 :(得分:0)

您的代码中存在两个问题。

首先,如果你想调用你的功能

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;
}

您应该提供两个参数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;
}

然后,在main函数中,您的调用函数语句是错误的。您可能希望收到返回值。所以,你应该写这样的代码。

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

    double ret = moon_g();
}

最后,大多数建议应该先声明或定义另一个函数调用的函数。

相关问题