3

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.

Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
user3150381
  • 41
  • 1
  • 1
  • 2

4 Answers4

5

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.

Beta
  • 86,746
  • 10
  • 132
  • 141
  • "declare the function before you call it" is something that bites a lot of rookie C++ devs. – WEFX Oct 20 '20 at 13:05
2

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.

Community
  • 1
  • 1
Shafik Yaghmour
  • 143,425
  • 33
  • 399
  • 682
1

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();
}
PTwr
  • 1,105
  • 1
  • 10
  • 16
  • thank you! this worked perfect! So, i'm learning by the online tutorial c++.com, and I'm on the "declaring functions" section right now. It says that the actual place of the main() function within the code doe'snt matter since main is always called first. So is it wrong that they say that because writing the moon_g() before the main seemed to make a difference. – user3150381 Jan 04 '14 at 05:00
  • Location of main() does not matter, location of functions that you call from main() matters. C++ source code files are compiled "line by line", so you first need to declare at least header of function before you can call it. In, for example, C# such details do not matter as it is modern language while C/C++ is still holding to ancient limitations of 4KB ram which not allowed to read whole file to memory at once. (One could say that split to .h and .cpp is making code more clear to read, but on other hand advanced IDE provide tools to manage code) – PTwr Jan 04 '14 at 14:11
  • @user3150381 cplusplus.com is often hastily written and not fact checked. It is not a wiki. Avoid relying on it. – Potatoswatter Jan 05 '14 at 01:49
0

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. But a and b are calculated in the body of function definition, it is unnecessary to declare the two parameters. 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. 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.

Jeff
  • 313
  • 1
  • 12
  • Thank you very much for your reply! i tried to run the program exactly as you pointed out. here is what happened: 1. double moon_g(), with no arguments double a and double b, gives me an error:a and b were not declared in this scope. If I put those arguments back into double moon_g() and run it, it gives me an error saying: too few arguments for double ret=moon_g(). I don't know what I'm doing wrong. I tried to compile it Dev C++. – user3150381 Jan 04 '14 at 04:49
  • Oh, my mistake, you should declare the `a` and `b` in the definition body of the function instead of in the arguments list. I edit my code again. sorry for my mistake. – Jeff Jan 04 '14 at 05:05
  • In addition, `double ret = moon_g()`, `ret` will store the return value calculated by moon_g(); – Jeff Jan 04 '14 at 05:11