В коде есть две проблемы.
Во-первых, если вы хотите назвать вашу функцию
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;
}
вы должны предоставить два параметра a
и b
. Но a
и b
рассчитываются в теле определения функции, нет необходимости объявлять два параметра. Вы можете написать вот так.
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;
}
Тогда в основной функции оператор вашей вызывающей функции ошибочен. Вы можете получить возвращаемое значение. Итак, вы должны написать такой код.
int main()
{
cout<<"This program will calculate the weight of any mass on the moon\n";
double ret = moon_g();
}
Наконец, в основном рекомендуется, чтобы функция, которая будет вызываться другой функцией, должна быть объявлена или определена ранее.
Возможный дубликат [The Definitive C++ Book Guide and List] (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) –
Благодарю всех вас за помощь! – user3150381
Итак, единственный способ вызвать функцию в C++ из основной функции - cout << определенная функция? – user3150381