# I Need Help Defining and Calling Functions

• 10-24-2007
jonbuckets
I Need Help Defining and Calling Functions
Here is a link to what I am trying to do... I'm not sure what I am doing wrong.

http://ezekiel.vancouver.wsu.edu/~cs...ts/pdf/pdf.pdf

Here is my source code thus far...

Code:

```#include<stdio.h> #include<stdlib.h> #include<math.h> #define INV_SQRT_2PI 0.39894228  /* 1/sqrt(2*pi) */ #define epsilon 0.0000001 double a, b, approx, u, U, O; int x; double o=epsilon; double f(int x) {                double f(int x); = exp(((double)x*x)/2);         return f(x); } double S(double a, double b) {         double S(double a, double b); = ((b-a)/6)*(f(a)+4*f((a+b)/2)+f(b));         return S(a,b); } double asimpson(double a,double b,double approx, double o) {         double c=(a+b)/2;         double left=S(a,c);         double right=S(c,b);         double d=(left+right-approx)/15;         if(abs(d)<=epsilon)                 return left+right+d;         return asimpson(a,c,left,o/2)+asimpson(c,b,right,o/2); } int main(void) {                scanf("%lf", &x);         scanf("%lf", &U);         scanf("%lf", &O);         double p, u, o;         u=(x-U)/O;         p=(1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),o);         printf("pdf= %f",p); return 0; }```
Values of x=72, U=69, and O=2.8 should give a p=0.85801

You'll all probably find it disgustingly crude, but I've never done any programming before... please be kind and help a newbie.
• 10-24-2007
jonbuckets
I think I might have it a little better now. I'm getting the error code "function-style initializer appears to be a function definition" on line 27, 31, and 35. How do I fix this?

Code:

```#include<stdio.h> #include<stdlib.h> #include<math.h> #define INV_SQRT_2PI 0.39894228  /* 1/sqrt(2*pi) */ #define epsilon 0.0000001 double a, b, u, U, O, c, left, right, d, approx; int x; double o=epsilon; double f(int x); double S(double a, double b); double asimpson(double a, double b, double approx, double o); int main(void) {                scanf("%lf", &x);         scanf("%lf", &U);         scanf("%lf", &O);         double p, u;         u=(x-U)/O;         p=(1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),epsilon);         printf("pdf= %f",p); return 0; } double f(x) {                return exp(double(x*x)/2); } double S(a,b) {         return ((b-a)/6)*(f(a)+4*f((a+b)/2)+f(b)); } double asimpson(a,b,approx,epsilon) {         approx= S(a,b);         c=(a+b)/2;         left=S(a,c);         right=S(c,b);         d=(left+right-approx)/15;         if(abs(d)<=epsilon)                 return left+right+d;         return asimpson(a,c,left,o/2)+asimpson(c,b,right,o/2); }```
• 10-24-2007
dwks
It helps to use colour or comments or something to indicate exactly which lines are 27 etc, that way we don't have to copy the code into an editor. ;)

Code:

```double f(x) {     /* ... */ }```
You got the prototype right.
Code:

`double f(int x);`
Now put that type into the function definition as well, and you're all set.
• 10-24-2007
jonbuckets
One more thing...
Well, it compiles, and runs sucessfully now, but I don't quite think I have the calls done correctly. The only warnings I'm getting are due to the scanf's. The pdf needs to call on the double asimpson(double a,double b,double approx, double epsilon), which calls on the double S(double a,double b), and that calls on the double f(double x)

If someone could tell me if I have all this coded correctly that would be great. I have until Midnight to try and get it working as best I can.

Code:

```#include<stdio.h> #include<stdlib.h> #include<math.h> double INV_SQRT_2PI= 0.39894228;                                                /*1/sqrt(2*pi)*/ double a, b, u, U, O, c, left, right, d, approx, x; double epsilon= 0.0000001;                                                                /*epsilon defined as 0.0000001*/ double f(double x);                                                                                /*prototype functions*/ double S(double a, double b); double asimpson(double a, double b, double approx, double epsilon); int main(void) {                printf("Enter x Value: ");         scanf("%lf", &x);         printf("Enter Mean: ");         scanf("%lf", &U);         printf("Enter Standard Deviation: ");         scanf("%lf", &O);         double pdf, u;         u=((x-U)/O);         pdf=((1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),epsilon));         printf("pdf= %f",pdf); return 0; } double f(double x){                return (exp(double(x*x*(-1))/2)); } double S(double a,double b){                return ((b-a)/6)*(f(a)+(4*f((a+b)/2))+f(b)); } double asimpson(double a,double b,double approx, double epsilon) {         approx= S(a,b);         c=((a+b)/2);         left=S(a,c);         right=S(c,b);         d=((left+right-approx)/15);         if(abs(d)<=epsilon)                 return (left+right+d);         return (asimpson(a,c,left,epsilon/2)+asimpson(c,b,right,epsilon/2));}```
• 10-25-2007
Salem
It would help if you didn't have a bunch of global variables with the same names as parameters to your functions.

Also, apart from declaring variables in the middle of code, there is nothing here which makes this a C++ program (it's all C).

> double INV_SQRT_2PI= 0.39894228;
This isn't used - remove it.
• 10-25-2007
matsp
Code:

`        pdf=((1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),epsilon));`
But it should be made "const", since it's not changing.

--
Mats
• 10-25-2007
Salem
Apart from Daniweb, how many other forums is this on?