Question:
Having trouble calling a function in C, please help!?
bobbythompson
2009-11-30 18:57:39 UTC
int direction(int orientation, int direction)
{
int newdirection;

//no mirror present
if(orientation==0 && direction==6)
newdirection=6;

else if(orientation==0 && direction==3)
newdirection=3;

else if(orientation==0 && direction==9)
newdirection=9;

else if(orientation==0 && direction==12)
newdirection=12;

return newdirection;
}

main()
{
//declare variables
int orientation, direction, x, y, dir;
x = 0;
y = 1;

dir = direction(x, y);//call function
printf("%d", dir);
return 0;
}

I keep receiving the following error:
error: called object is not a function
Five answers:
MichaelInScarborough
2009-11-30 19:01:23 UTC
That is because of your locally declared variable called int direction.

:Oo
Ratchetr
2009-11-30 19:06:40 UTC
The compiler sees:



// A function named direction:

int direction(int orientation, int direction)



// A variable named direction, in main

int orientation, direction, x, y, dir;



// A function call to direction.

dir = direction(x, y);//call function



When the compiler does a lookup on direction, it finds the 'nearest' definition. In this case, it's the variable of type int defined in main. So it thinks you want to call an int variable as a function.

Can't do that.



The direction variable isn't being used. Get rid of it.



The direction function takes a parameter named direction. That should be OK, but it's confusing. Try to keep function names unique from variable names.
The Phlebob
2009-11-30 19:25:53 UTC
This code:



int orientation, direction, x, y, dir; <===========

x = 0;

y = 1;



dir = direction(x, y);//call function



redefines direction for the main() function, overriding the earlier direction() function definition.



Hope that helps.
2009-11-30 19:02:28 UTC
You defined a variable called "direction" in the main method, which hides the direction method.

Change this:

int orientation, direction, x, y, dir;

into this:

int orientation, x, y, dir;
2016-11-14 11:34:43 UTC
>>you haven't any longer declared "sum" and "var" as variables so write a assertion like int sum=0,var until now the for loop. >>printf(“The sum of a million to %i is: %i n”, a, result); i think of its %d no longer %i >>i may well be sturdy in case you are able to furnish the specific errors brought about by skill of the compiler while this technique is administered ............want you good fortune...hopes it ought to make it easier to...


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...