# Thread: Calling functions, calculating integral

1. ## Calling functions, calculating integral

I'm trying to call multiple functions to calculate an integral on the interval 0 < a < b.

My code compiles and it seems to call up the functions correctly but I get a weird number for an answer.

This is my code:
Code:
```#include <stdio.h>
#include <math.h>

int main()
{
int n;
double m, A, a, b;
printf("\nEnter the exponents (double)m and (int)n in f(x)=A*x^m/n! : ");
scanf("%lf %lf", &m,&n);
printf("\nEnter the coefficient A in f(x)=A*x^m/n! : ");
scanf("%lf",&A);
printf("\nEnter bounds for integration interval, a < b : ");
scanf("%lf %lf", &a,&b);
Intgr(m,n,A,a,b);
}

double Intgr(double m, int n, double A, double a, double b)
{
int n_trap, k;
double del_x, x, f, sum;
printf("\nIntegrate f(x) on [a,b]\n");
printf("Enter the number of trapezoids: ");
scanf("%d", &n_trap);
del_x = (b-a)/(double)n_trap;
x = a;
f = func(x, m, n, A);
sum = - 0.5 * f;
for(k=0; k<=n_trap; k++)
{
x = a + k * del_x;
f = func(x, m, n, A);
sum += f;
}
sum -= 0.5 * f;
sum *= del_x;
printf("\nIntegral=%f\n", sum);
}

double func(double x, double m, int n, double A) {
if (n == 0)
return A * pow(x,m);
else
return (A* pow(x,m)) / nfact(n);
}

double nfact(int n){
double f=1;
f*=n--;
return f;
}```
This is my output:
Code:
```Enter the exponents (double)m and (int)n in f(x)=A*x^m/n! : 3.25 5

Enter the coefficient A in f(x)=A*x^m/n! : -1.5

Enter bounds for integration interval, a < b : 1.5 3.75

Integrate f(x) on [a,b]
Enter the number of trapezoids: 1000

Integral=2416590846.654750```
The value of the integral should be -0.792905 .

I'm getting the following warnings when I compile:
hw7.c:19: warning: type mismatch with previous implicit declaration
hw7.c:15: warning: previous implicit declaration of `Intgr'
hw7.c:19: warning: `Intgr' was previously implicitly declared to return `int'
hw7.c:40: warning: type mismatch with previous implicit declaration
hw7.c:27: warning: previous implicit declaration of `func'
hw7.c:40: warning: `func' was previously implicitly declared to return `int'
hw7.c:47: warning: type mismatch with previous implicit declaration
hw7.c:44: warning: previous implicit declaration of `nfact'
hw7.c:47: warning: `nfact' was previously implicitly declared to return `int'

What do these errors mean and how do I make my Intgr function return the right value? Thank you very much.

2. You cannot (ok, should not, but look what happens) call a function without a prototype. Put a prototype of each function before int main(). Also, I really hope you don't believe that nfact() computes n! -- and also, it might be nice to just compute it once somewhere, since it's not as though it's changing.

3. this is my modified code (declared prototypes, tried to fix the factorial equation):
Code:
```#include <stdio.h>
#include <math.h>

double Intgr(double m, int n, double A, double a, double b);
double func(double x, double m, int n, double A);
double nfact(int n);

int main()
{
int n;
double m, A, a, b;
printf("\nEnter the exponents (double)m and (int)n in f(x)=A*x^m/n! : ");
scanf("%lf %lf", &m,&n);
printf("\nEnter the coefficient A in f(x)=A*x^m/n! : ");
scanf("%lf",&A);
printf("\nEnter bounds for integration interval, a < b : ");
scanf("%lf %lf", &a,&b);
Intgr(m,n,A,a,b);
}

double Intgr(double m, int n, double A, double a, double b)
{
int n_trap, k;
double del_x, x, f, sum;
printf("\nIntegrate f(x) on [a,b]\n");
printf("Enter the number of trapezoids: ");
scanf("%d", &n_trap);
del_x = (b-a)/(double)n_trap;
x = a;
f = func(x, m, n, A);
sum = - 0.5 * f;
for(k=0; k<=n_trap; k++)
{
x = a + k * del_x;
f = func(x, m, n, A);
sum += f;
}
sum -= 0.5 * f;
sum *= del_x;
printf("\nIntegral=%f\n", sum);
}

double func(double x, double m, int n, double A) {
if (n == 0)
return A * pow(x,m);
else
return (A* pow(x,m)) / nfact(n);
}

double nfact(int n){
int t = 1;
int as;
for (as = 1; as<=n; as++) {
t *=as;
}
return t;
}```
It compiles with no warnings but my output seems to stop after I "Enter the number of trapezoids : 1000".
My output:
Code:
```Enter the exponents (double)m and (int)n in f(x)=A*x^m/n! : 3.25 5

Enter the coefficient A in f(x)=A*x^m/n! : -1.5

Enter bounds for integration interval, a < b : 1.5 3.75

Integrate f(x) on [a,b]
Enter the number of trapezoids: 1000```
And it freezes right there. Why is it not calculating my integral?

4. Well you could try beginning with indentation, so that people might want to look at your code. It's horrible.

Here, consider how much easier this is to follow.
Code:
```#include <stdio.h>
#include <math.h>

double Intgr(double m, int n, double A, double a, double b);
double func(double x, double m, int n, double A);
double nfact(int n);

int main()
{
int n;
double m, A, a, b;
printf("\nEnter the exponents (double)m and (int)n in f(x)=A*x^m/n! : ");
scanf("&#37;lf %lf", &m,&n);
printf("\nEnter the coefficient A in f(x)=A*x^m/n! : ");
scanf("%lf",&A);
printf("\nEnter bounds for integration interval, a < b : ");
scanf("%lf %lf", &a,&b);
Intgr(m,n,A,a,b);
//!! what returns here?
}

double Intgr(double m, int n, double A, double a, double b)
{
int n_trap, k;
double del_x, x, f, sum;
printf("\nIntegrate f(x) on [a,b]\n");
printf("Enter the number of trapezoids: ");
scanf("%d", &n_trap);
del_x = (b-a)/(double)n_trap;
x = a;
f = func(x, m, n, A);
sum = - 0.5 * f;
for(k=0; k<=n_trap; k++)
{
x = a + k * del_x;
f = func(x, m, n, A);
sum += f;
}
sum -= 0.5 * f;
sum *= del_x;
printf("\nIntegral=%f\n", sum);
//!! what returns here?
}

double func(double x, double m, int n, double A) {
if (n == 0)
return A * pow(x,m);
else
return (A* pow(x,m)) / nfact(n);
}

double nfact(int n){
int t = 1;
int as;
for (as = 1; as<=n; as++) {
t *=as;
}
return t;
}```

5. As Salem identified, you have two functions that don't return, also, there is not much use having Intgr return a double when you don't use its value. Either think of a value to return or return void, but always return something.