Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function in main

Tags:

c++

function

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

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.

like image 801
user3150381 Avatar asked Sep 26 '26 22:09

user3150381


2 Answers

This line:

double moon_g();

doesn't actually do anything, it just states that a function double moon_g() exists. 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). 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.

like image 64
Beta Avatar answered Sep 30 '26 12:09

Beta


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:

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:

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:

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. 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.

like image 37
Shafik Yaghmour Avatar answered Sep 30 '26 12:09

Shafik Yaghmour



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!