By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,066 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,066 IT Pros & Developers. It's quick & easy.

"variable cannot be used as a function" error

P: 1
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iosrteam>
  2. using namespace std;
  3. int what;
  4. int main()
  5. {
  6.   int what;
  7.   cout << what(9,3,17) << endl;
  8. }
  10. int what(int x, int y, int z)
  11. {
  12.     if(x>y && x>z)
  13.       return x;
  14.    if(y>x && y>z)
  15.      return y;
  16.    if(z>x && z>y)
  17.     return z;
  18.   else
  19.    return 0;
  20. }
I feel like its a simple fix... help!
Feb 19 '16 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 5K+
P: 9,197
Starting from the top of the program, the first thing the compiler sees is:

Expand|Select|Wrap|Line Numbers
  1. int what;
The next thing the compiler sees is:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2.  {
  3.  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:

Expand|Select|Wrap|Line Numbers
  1. 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:

Expand|Select|Wrap|Line Numbers
  1. int what(int x, int y, int z);
  3. int main()
  4. {
  5.     what(9,3,17);   // this is now OK
  6. }
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.
Feb 19 '16 #2

P: 1
You also wrote #include <iostream> wrong
Jan 20 '20 #3

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

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   int what;
  4.   cout << what(9,3,17) << endl;
  5. }

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

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   int what(int,int,int);
  4.   cout << what(9,3,17) << endl;
  5. }
Feb 13 '20 #4

Post your reply

Sign in to post your reply or Sign up for a free account.