简体   繁体   English

在main中调用函数

[英]Calling a function in main

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

but doesn't execute the moon_g function. 但不执行moon_g函数。

This line: 这一行:

double moon_g();

doesn't actually do anything, it just states that a function double moon_g() exists. 它实际上没有做任何事情,它只是声明一个函数double moon_g()存在。 What you want is something like this: 你想要的是这样的:

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) . 这还行不通,因为你没有函数double moon_g() ,你所拥有的是一个函数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: 所以从你的函数中消除它们是这样的:

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. (并在调用之前声明该函数。)可以进行更多细化,但现在已经足够了。

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_g before main or add a forward declaration before main like this: 它不会工作,因为你要么需要移动的定义moon_gmain或前加一个向前声明main是这样的:

double moon_g (double a, double b) ;

Although it seems like a and b are not inputs but values you want to return back to main then 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 clang provides the following warning: 您使用哪种编译器会产生影响clang提供以下警告:

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

while I can not get gcc nor Visual Studio to warn me about this. 虽然我不能让gccVisual Studio警告我这件事。 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 . 从长远来看,在不同的C ++编译器中尝试代码是很有用的,它可以是一种非常有教育意义的体验,并且您不必安装它们,因为在线有大量的在线C ++编译器

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

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 a and b . 你应该提供两个参数ab But a and b are calculated in the body of function definition, it is unnecessary to declare the two parameters. 但是ab是在函数定义的主体中计算的,没有必要声明这两个参数。 You can write like this. 你可以写这样的。

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. 然后,在main函数中,您的调用函数语句是错误的。 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. 最后,大多数建议应该先声明或定义另一个函数调用的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM