# Thread: Problem with defining function

1. ## Problem with defining function

ok here's what i have done;

#define a(r) ((4)*(1/r)*(1-(1/r)));

using it like this:

tot = tot + a(r);

But while compiling i get :

invalid operands to binary +

Can anyone suggest me what am i doing wrong?

2. You've got an error that doesn't cause an error (here): this line now ends in two semicolons (one from the #define, one from the line itself).

What is r, then? I hope it's a floating-point thing, otherwise 1/r could get tricky.

And why not use a function instead of a pseudofunction?

3. r is double and i mistype it i don't have ";" in my real code and 4 is 4.0 and 1 is 1.0 is my code. I am just trying to this pseudofunction .

4. Originally Posted by tabstop
You've got an error that doesn't cause an error (here): this line now ends in two semicolons (one from the #define, one from the line itself).
What tabstop means is don't put a semi-colon after a #define statement.

5. r is double and i mistype it i don't have ";" in my real code and 4 is 4.0 and 1 is 1.0 is my code. I am just trying to this pseudofunction .

6. Originally Posted by curious
r is double and i mistype it i don't have ";" in my real code and 4 is 4.0 and 1 is 1.0 is my code. I am just trying to this pseudofunction .
Cut, paste, and post the appropriate parts of your real code then.

7. #define a(r) (-4.0)*(1.0/(r))*((1.0/(r))-1.0)

And there is subroutine
which uses this function

tot = tot + a(r);

8. Minimum sample

Code:
```#define a(r) (-4.0)*(1.0/(r))*((1.0/(r))-1.0)

int main(void)
{
double tot = 0;
double r = 10;
tot = tot + a(r);
return 0;
}```
Compiles fine.

Are you sure there are not problems on previous lines? Are you sure the compiler error is referring to this line? Are you sure your types are OK, e.g tot is not char* or whatever?

9. I have also called the same function in main and it's works fine but when i tired to call it in a subroutine it didn't work at all

10. Originally Posted by curious
I have also called the same function in main and it's works fine but when i tired to call it in a subroutine it didn't work at all
Cut, paste, and post the subroutine where you call it.

11. And remember, the file is parsed from top to bottom, so attempting to call a macro above where it is defined simply won't work.

12. @tabstop: how to do it then?

13. Define it before you use it. (This seems a bit too obvious, but is correct nevertheless.)

14. ok the basic structure is :
Code:
```#define ......

int main()
{
}

bool func() /**bool is taken care of and works fine**/
{

defined function is called here as explained above
}```

15. @tabstop :defined it but it didn't work