Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Functions - Error: '0' cannot be used as a function

Tags:

c++

I'm new to functions and trying to understand what I've done wrong. My build message spits out the error: '0' cannot be used as a function and highlights the line return ((5 / 9)(fahrenheit - 32)); within the function. Thanks in advance for any advice.

#include <iostream>
using namespace std;

double celsiusFunction(double fahrenheit);

int main()
{
    double fahrenheitTemp;

    fahrenheitTemp = celsiusFunction(99);
    cout << fahrenheitTemp;

    return 0;
}

double celsiusFunction(double fahrenheit)
{
    return ((5 / 9)(fahrenheit - 32));
}
like image 733
Thall Avatar asked Jan 16 '26 21:01

Thall


1 Answers

  1. 5 / 9 is 0, because both are integers and thus it's evaluated in integer arithmetic. Do this instead: 5.0 / 9.0 to get floating results.

  2. You're not multiplying in the return statement, so the compiler interprets the second parentheses as a funciton call (that is, calling 5 / 9 with arguments fahrenheit - 32). This is of course nonsense. Do this:

    return (5.0 / 9.0) * (fahrenheit - 32.0);
    
like image 63
Angew is no longer proud of SO Avatar answered Jan 19 '26 15:01

Angew is no longer proud of SO



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!