By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,515 Members | 1,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,515 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:

#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;
return 0;

I feel like its a simple fix... help!
Feb 19 '16 #1
Share this Question
Share on Google+
1 Reply

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

Post your reply

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