Calling a function in main
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.
Solution 1:
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.
Solution 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.
Solution 3:
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();
}