# Function pointer problem

Page 1 of 1

## 5 Replies - 427 Views - Last Post: 23 September 2009 - 07:58 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=127491&amp;s=d83d4e86a1e30aab72be0df5d1a86e7f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 aries0152

Reputation: 0
• Posts: 24
• Joined: 13-September 09

# Function pointer problem

Posted 23 September 2009 - 12:11 AM

I like to solve a eqn using pointer to function. But I am having some problem in using it.

My code is-

``` #include<iostream>
#include<cmath>

using namespace std;

double func(double t,double f)
{
return 1.0/sqrt(1.0-pow((sin(t/2.0)*sin(f)),2));
}

double simp(double(*f)(double),double a,double b,int n)
{
int i;
double sum,ini,h;
i=0;sum=0.0;ini=0.0;
h=(b-a)/n;
ini+=(*f)(a);
ini+=(*f)(b);
for(i=1;i<=n/2.0;i++)
{
ini+=4*((*f)(a+((2*i)-1)*h));
ini+=2*((*f)(a+((2*i)-2)*h));
}
sum=(1.0/3.0)*h*ini;
return sum;
}

int main()
{
int i=10000;
double a=0.0,b=M_PI/2.0;

cout<<"The integral is "<<simp(func,a,b,i)<<endl;
return 0;
}
```

But when I complied this code it shows -

In function ‘int main()’:
error: invalid conversion from ‘double (*)(double, double)’ to ‘double (*)(double)’
error: initializing argument 1 of ‘double simp(double (*)(double), double, double, int)’

I am a beginner in function pointer . So please suggest me how do I call the function to get rid of this error??

Is This A Good Question/Topic? 0

## Replies To: Function pointer problem

### #2 carltech

• What did you call me?

Reputation: 28
• Posts: 997
• Joined: 19-October 07

## Re: Function pointer problem

Posted 23 September 2009 - 01:09 AM

What are you trying to do? are you trying to pass a pointer or pass a function to the function?

if you want to pass a pointer then it should be double simp(double *f,double a,double b,int n)
Then you would have problems with the compiler thinking that you are using f as a function. ini+=(*f)(a); looks like a is being passed to f. if you meant to do multiplycation then use *.

If you meant to pass a function then myb.

### #3 aries0152

Reputation: 0
• Posts: 24
• Joined: 13-September 09

## Re: Function pointer problem

Posted 23 September 2009 - 01:44 AM

carltech, on 23 Sep, 2009 - 12:09 AM, said:

are you trying to pass a pointer or pass a function to the function?

I want to pass the function

### #4 aks29921

Reputation: 84
• Posts: 230
• Joined: 24-August 09

## Re: Function pointer problem

Posted 23 September 2009 - 02:03 AM

if a function has two parameters, you should always pass them to it, this was one mistake committed by you.
here you go:
```#include<iostream.h>
#include<math.h>

double func(double t,double f)
{
return 1.0/sqrt(1.0-pow((sin(t/2.0)*sin(f)),2));
}

double simp(double(*f)(double,double),double a,double b,int n)
{
int i;
double sum,ini,h;
i=0;sum=0.0;ini=0.0;
h=(b-a)/n;
ini+=f(a,b);
for(i=1;i<=n/2.0;i++)
{
ini+=4*(f(a+((2*i)-1),h));
//ini+=2*((*f)(a+((2*i)-2)*h));
}
sum=(1.0/3.0)*h*ini;
return sum;
}

int main()
{
int i=10000;
double a=0.0,b=M_PI/2.0;

cout<<"The integral is "<<simp(func,a,b,i)<<endl;
return 0;
}

```

this program compiles and runs properly..
the output may not be what you desire, that's because i have not tried to understand what you are doing but have just corrected your syntax in certain places.
you can build on this code to do exactly what you want...

This post has been edited by aks29921: 23 September 2009 - 03:03 AM

### #5 aries0152

Reputation: 0
• Posts: 24
• Joined: 13-September 09

## Re: Function pointer problem

Posted 23 September 2009 - 07:36 AM

Error solved. Thank you

### #6 NickDMax

Reputation: 2222
• Posts: 9,208
• Joined: 18-February 07

## Re: Function pointer problem

Posted 23 September 2009 - 07:58 AM

nice job aks29921

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }