-1

As you can probably tell by my code, I'm a C++ beginner, practicing functions/return statements. I've attempted to create a basic program that calculates the area of a circle using the radius, which is given through user input. For some reason, the program always returns a 1? No doubt I've missed something simple, but for the life of me I can't see any problems?

#include<iostream>
#include<math.h>
using namespace std;

double areaofcircle(double Radius) {
    return M_PI * pow(Radius, 2);
}

int main () {
    double Radius;

    cout << "Enter the radius of your circle \n";
    cin >> Radius;
    cout << "The area of your circle equals  " << areaofcircle;

    return 0;
}
mch
  • 9,424
  • 2
  • 28
  • 42
Ryan
  • 1
  • 1
  • 1
    Listen to the compiler warnings; `areaofcircle` is treated as a function pointer: https://godbolt.org/z/rE55Kz8vr You need to call the function and I recommend printing a newline at the end of the output: `... << areaofcircle(Radius) << '\n';` – fabian Nov 30 '22 at 08:59
  • tip: `std::cout` can swallow almost anything and may trigger unexpected conversions. You get a nice compiler error if instead you write `double a = areaofcircle; std::cout << a;` – 463035818_is_not_an_ai Nov 30 '22 at 09:04
  • For an even more bizarre-looking result, try `cout << "The area of your circle equals " << std::boolalpha << areaofcircle;` (For something less bizarre, [enable warnings](https://stackoverflow.com/questions/57842756/why-should-i-always-enable-compiler-warnings).) – JaMiT Dec 01 '22 at 09:36

1 Answers1

0

Like this

cout << "The area of your circle equals  " << areaofcircle(Radius);

In C++ when you call a function you must use parentheses () and any parameters to the function are placed between the parentheses separated by commas.

You actually know this because you did the right thing when you called pow, but not when you called your own function. The rules are the same, whether it's your function or one of the standard library functions.

john
  • 85,011
  • 4
  • 57
  • 81