"variable cannot be used as a function" error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cpsc122
    New Member
    • Feb 2016
    • 1

    "variable cannot be used as a function" error

    I'm writing a very simple program, but I keep getting the same error, no matter what I do. What is it that is going wrong? It says I can't use the variable as a function.

    Here is my code:

    Code:
    #include <iosrteam>
    using namespace std;
    int what;
    int main()
    {
      int what;
      cout << what(9,3,17) << endl;
    }
    
    int what(int x, int y, int z)
    {
        if(x>y && x>z)
          return x;
       if(y>x && y>z)
         return y;
       if(z>x && z>y)
        return z;
      else
       return 0;
    }
    I feel like its a simple fix... help!
    Last edited by gits; Jan 20 '20, 10:26 AM. Reason: added code tags
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Starting from the top of the program, the first thing the compiler sees is:

    Code:
    int what;
    The next thing the compiler sees is:

    Code:
    int main()
     {
     int what;
    This is a second variable named what but this one is local to main(). The int what outside of main is completely blocked by this local one.

    Then the compiler sees:

    Code:
    cout << what(9,3,17) << endl;
    so you get an error trying to use variable as a function. There is a what function, but the compiler hasn't seen it yet.

    YOu need a function prototype:

    Code:
    int what(int x, int y, int z);
    
    int main()
    {
        what(9,3,17);   // this is now OK
    }
    The function prototype is the first line of the function definition ended by a semicolon. This tells the compiler there is a function what with 3 int arguments.

    Lastly, never name your variables and your functions using the same name.

    Comment

    • MegaCharizardX
      New Member
      • Jan 2020
      • 1

      #3
      You also wrote #include <iostream> wrong

      Comment

      • Ishan Shah
        New Member
        • Jan 2020
        • 47

        #4
        You have declared variable which name is the same as the function name, so that's why it will give you an error

        Code:
        int main()
        {
          int what;
          cout << what(9,3,17) << endl;
        }

        if you want to declare the function, then you can write like the following code instead of that

        Code:
        int main()
        {
          int what(int,int,int);
          cout << what(9,3,17) << endl;
        }

        Comment

        • ellavaughn
          New Member
          • May 2020
          • 1

          #5
          thanks for taking the time to explain.

          Comment

          • AjayGohil
            New Member
            • Apr 2019
            • 83

            #6
            Hi,

            In your program you use what as a function and also as a variable and in function declaration there is no parameter and in a function call you pass three parameter.so you have to add three parameter to function declaration.

            Thank you

            Comment

            Working...