Function pointers: trying to pass a function as an argument

Hello, my program works ok when I pass to the function simpson all four parameters as doubles and ints, but because I need the last parameter to be passed as a function, I get errors. I have written problem_13.cpp and simpson.h programs. I think I understand pointer functions, but they seem to not work for me. The errors I get are from these lines:
f = simpson(a,b,n,function(fk_1)); from problem_13.cpp line 26
f = simpson(a,b,n,function(fk_2)); from problem_13.cpp line 31
double simpson(double a, double b, int n, double function); from simpson.h file line 8
if (function(fk_1) == true); from simpson.h file line 12
if (function(fk_2) == true); from simpson.h file line 20

Please help me to understand how to solve these errors and make a function passed as a parameter to simpson() where I need the 4th parameter to be fk_1() or fk_2(). Looking forward to all responses.

problem_13.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cmath>
#include <cstdlib>
#include "simpson.h"

void text();

int main()
{
double a, b, f;
int n, m;

printf("a = "); // choose the interval start
scanf("%lf", &a);
printf("b = "); // choose the interval end
scanf("%lf", &b);
printf("n = "); // choose how many steps
scanf("%d", &n);
printf("\n");

  text(); // show the menu
  scanf("%d", &m); // choose which function from text() to integrate
  switch (m)
  {
    case 1:
      f = simpson(a,b,n,fk_1); // should calculate the integral of the first function
      printf("Integral = %3.3f \n", f); // should give the integral of the first function
      break;

    case 2:
      f = simpson(a,b,n,fk_2); // should calculate the integral of the second function
      printf("Integral = %3.3f \n", f); // should give the integral of the second function
      break;

    case 3:
    printf("Quiting...\n"); // quits the program
    return 0;
    break;

    default:
    printf("Incorrect choices.\n"); // if pressed anything ecept 1,2 or 3 gives a menu text again
    text();
    break;
  }
}

void text()
{
  printf("Meniu\r\n\n");
  printf("Which function to integrate?\n");
  printf("f(x) = x - press 1\n");
  printf("f(x) = x^2 - press 2\n");
  printf("Leave menu - press 3\n");
  printf(": ");
}


simpson.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cmath>

double function(double, double (*f)(double));
double fk_1(double);
double fk_2(double);

double simpson(double a, double b, int n, double function);
{
  double sum = 0.0; // base length 
  double h2 = h / 2; // half base length
    if (function(fk_1) ==  true)
    {
    for (int i = 0; i < n; i++)
    {
    double left = a + i * h; // left side of the simpson's rule to calculate integral of a function
    sum += fk_1(left) + 4 * fk_1(left + h2) + fk_1(left + h); // remaining stes of the simpson's rule to calculate the integral of a function
    }
    }
    else if (function(fk_2) == true) {
    for (int i = 0; i < n; i++)
    {
    double left = a + i * h; // look above
    sum += fk_2(left) + 4 * fk_2(left + h2) + fk_2(left + h); // look above
    }
    }
  // integral calculation
  return h / 6 * sum;
}

double function(double x, double (*f)(double))
{
  return (*f)(x);
}

double fk_1(double x)
{
  return x;
}

double fk_2(double x)
{
  return (x * x);
}


P.S. If you need more information - let me know and I'll provide. Also any reading material suggestions are welcome.
Last edited on
Have you tried to put your function implementations in a source file (.cpp)? Unless dealing with templates it is usually a bad practice to have your implementations in a header file.

The errors I get are from these lines:

Posting the actual error messages, all of them, exactly as shown in your development environment would be better than posting random lines of code.

Also using meaningful variable and function names would also be helpful.

And remember the function "function()" requires two parameters.

Please do not doublepost. The other thread: http://www.cplusplus.com/forum/general/241781/
Sorry keskiverto. I won't double post again. I just didn't know where to post my question :)
I can't add simpson function to my source.cpp file. it has to come from other .cpp, h. or obj. file. My problem says that simpson function has to be written outside the source .cpp file and in .cpp it should be included and called onto as I have implemented above.

The errors I get at the moment are:
1
2
3
4
./simpson.h:8:43: error: C++ requires a type specifier for all declarations
double simpson(double a, double b, int n, function(double, double (*f)(d...
./simpson.h:9:1: error: expected unqualified-id
{


Please respond here: http://www.cplusplus.com/forum/general/241781/
Topic archived. No new replies allowed.