# a tiny problem with a function

This is a discussion on a tiny problem with a function within the C Programming forums, part of the General Programming Boards category; hello, I'm new with c programming and... I'm trying to program a function which should perform the mathematical ditstance formula ...

1. ## a tiny problem with a function

hello,

I'm new with c programming and...

I'm trying to program a function which should perform the mathematical ditstance formula

but I 'm getting this error: found '{' at file scope (missing function header?

this is the code that I wrote

Code:
```#include <stdio.h>
#include <math.h>

float distance(float x1, float x2, float y1, float y2);
{
float a=0 ,b=0 ,c=0, d=0 e=0;
a= x2 -x1;
b= y2 -y1;
c = pow(a,2);
d = pow(b,2);

c+d=e

return sqrt(e);

}

int main()

{
x1=2; x2=4; y1=3; y2=6;

printf("%lf\n",distance(x1,x2,y1,y2);

return 0;
}```
what do I do wrong?

thenks for helping..

2. Code:
```float distance(float x1, float x2, float y1, float y2);  //semicolon does not belong here, it thinks you're making a function prototype
{
float a=0 ,b=0 ,c=0, d=0 e=0;
a= x2 -x1;
b= y2 -y1;
c = pow(a,2);
d = pow(b,2);

c+d=e

return sqrt(e);

}```

3. Code:
`printf("%lf\n",distance(x1,x2,y1,y2);`
missing a parenthesis there.

4. Code:
`c+d=e`
pretty sure you want to do e=c+d

Also, you don't need to initialize all the local floats in distance() to 0, and you don't really need to use pow(,), you could just say c=a*a; and d=b*b;

5. Originally Posted by sing0
hello,

I'm new with c programming and...

I'm trying to program a function which should perform the mathematical ditstance formula

but I 'm getting this error: found '{' at file scope (missing function header?

this is the code that I wrote

Code:
```#include <stdio.h>
#include <math.h>

float distance(float x1, float x2, float y1, float y2);
{
float a=0 ,b=0 ,c=0, d=0 e=0;
a= x2 -x1;
b= y2 -y1;
c = pow(a,2);
d = pow(b,2);

c+d=e

return sqrt(e);

}

int main()

{
x1=2; x2=4; y1=3; y2=6;

printf("%lf\n",distance(x1,x2,y1,y2));

return 0;
}```
what do I do wrong?

thenks for helping..
Other error is in main, printf doesn't have a ending bracket.

6. I removed it( the semicolon)
and got lots of error

7. Code:
`	c+d=e`
This line should probably end with a ;

8. Code:
`x1=2; x2=4; y1=3; y2=6;`
you need to declare these as floats so that they'll work with your function and so they just have a type.
Code:
`float x1=2, x2=4, y1=3, y2=6;`

9. Originally Posted by sing0
I removed it( the semicolon)
and got lots of error
because it's actually parsing through the rest of the function now

10. Originally Posted by sing0
hello,

I'm new with c programming and...

I'm trying to program a function which should perform the mathematical ditstance formula

but I 'm getting this error: found '{' at file scope (missing function header?

this is the code that I wrote

Code:
```#include <stdio.h>
#include <math.h>

float distance(float x1, float x2, float y1, float y2);
{
float a=0 ,b=0 ,c=0, d=0 e=0;
a= x2 -x1;
b= y2 -y1;
c = pow(a,2);
d = pow(b,2);

c+d=e

return sqrt(e);

}

int main()

{
x1=2; x2=4; y1=3; y2=6;

printf("%lf\n",distance(x1,x2,y1,y2);

return 0;
}```
what do I do wrong?

thenks for helping..

So many basic problems in your code

dude so many i count so many

1.....
Code:
`float distance(float x1, float x2, float y1, float y2); // there is no need of ; here remove it`
2.....
Code:
`float a=0 ,b=0 ,c=0, d=0 e=0; // there is a need of , between d and e`
3.....
Code:
```c+d=e // dont know what you want to do it here may be you are doing e = c + d
e = c + d;```

4.....
Code:
```c+d=e // need ; after the statement
e = c + d;```
5.....
Code:
```float x1=2; x2=4; y1=3; y2=6; // here you need coma instead of semicolon
float x1=2, x2=4, y1=3, y2=6; // here you need coma instead of semicolon```

6.....
Code:
``` printf("%f\n",distance(x1,x2,y1,y2); // missing ) parenthesis
printf("%f\n",distance(x1,x2,y1,y2));```

11. thank u for the quick help

I fixed all the problems u told me but I still getting this error:
Code:
```1>c:\users\shir\documents\visual studio 2008\projects\try1\try1\try1.c(5) : error C2449: found '{' at file scope (missing function header?)
1>c:\users\shir\documents\visual studio 2008\projects\try1\try1\try1.c(19) : error C2059: syntax error : '}'```

12. Show us the modified code.

13. Originally Posted by sing0
I fixed all the problems u told me but I still getting this error:

14. Originally Posted by Epy
because it's actually parsing through the rest of the function now
a good one

15. Originally Posted by sing0
thank u for the quick help

I fixed all the problems u told me but I still getting this error:
Code:
```1>c:\users\shir\documents\visual studio 2008\projects\try1\try1\try1.c(5) : error C2449: found '{' at file scope (missing function header?)
1>c:\users\shir\documents\visual studio 2008\projects\try1\try1\try1.c(19) : error C2059: syntax error : '}'```
If your code is same which you posted first than you can refer to the solution provided by my fellow forum member's and if you changed the code than please post the code here

Page 1 of 2 12 Last