0

This is code that finds the root of any function that returns a double. I can't find the error, it keeps saying it expects "primary expression before double' the double being the double in front of the function I pass into the function. This is my first time trying to pass a function into a function, so I'm sure that's why I'm getting the error

 #include <cmath>
 #include <iostream>

double bisection(double start, double end, double tol, double &(f)(double x) ){


double x = end - start;
double num = f(x);
if( num < tol){
    return x;
}

if( num == 0){
    return x;
}

if( num < 0){
    start = num;
    bisection(start,end,tol, double &(f)(double x) );
}
else if(num > 0){
    end = num;
    bisection(start,end,tol, double &(f)(double x) );
    }
}

double fn(double x){
    return (  (1/sqrt(400 - x*x)) + (1/sqrt(900 - x*x))  - 0.1 )  ;
}
user121615
  • 199
  • 1
  • 7
  • The function argument is the only one where you include the type instead of just a value. I don't see why that one is different from the rest. – chris Mar 06 '15 at 01:02
  • I don't understand. Are you saying I shouldn't put the double in the front of the function I am passing by reference? – user121615 Mar 06 '15 at 01:05
  • @user121615: When you call a function, you don't specify the argument types, just the values. You got that right for the first three arguments, but for some reason wrote the type rather than the value for the fourth. The function call is `bisection(start,end,tol,f);` – Mike Seymour Mar 06 '15 at 01:06
  • ooooh. Wait, nevermind. I was thinking that was the synthax to pass by reference a function to another function. That's what I saw online. Imma recompile and see if it gets solved! – user121615 Mar 06 '15 at 01:06
  • @user121615, Passing by reference doesn't change the calling syntax. – chris Mar 06 '15 at 01:07
  • 1
    What's the purpose of that `bisection` above? You are not calling it anywhere from `main()`. – c-smile Mar 06 '15 at 01:07
  • I still have an error. Says "expected primary expression before double" – user121615 Mar 06 '15 at 01:10
  • @user121615: You're not passing a reference to a function, you're passing a pointer to a function that returns a reference. You probably want `double f(double)`, a (pointer to) a function returning a `double`, if you're planning to use `fn` as the argument. – Mike Seymour Mar 06 '15 at 01:10
  • I didn't create something that calls it yet. I just trying to get it to compile with the function. I should not have posted the main method. I did that by mistake – user121615 Mar 06 '15 at 01:11
  • 1
    @user121615: After fixing the dodgy function calls, the posted code [works for me](http://ideone.com/lwRarN). Which line causes that error? – Mike Seymour Mar 06 '15 at 01:12
  • Do you know any online places I can read about this at, Michael Seymour? Would double * f(double) fix it? – user121615 Mar 06 '15 at 01:12
  • Imma repost what I have now. – user121615 Mar 06 '15 at 01:13
  • When you call `bisection(start,end,tol, double &(f)(double x) );` just use `bisection(start,end,tol, f);` – Paul Rooney Mar 06 '15 at 01:14
  • @user121615: If you want a pointer to a function returning `double`, the syntax is `double (*f)(double)`, or just `double f(double)` if it's a function parameter (since function types are adjusted to pointer types in that context). The best way to learn C++ syntax is to read a [good book](http://stackoverflow.com/questions/388242). – Mike Seymour Mar 06 '15 at 01:15
  • I take it I'm not passing the function correctly. What would the correct synthax be that lets me actually use a function inside of the function? – user121615 Mar 06 '15 at 01:15
  • 1
    @user121615: As I said earlier, `bisection(start,end,tol,f)` – Mike Seymour Mar 06 '15 at 01:16
  • Oooooh, I'm sorry, I didn't understand what you were saying. Thanks for the help, it finally compiled. I kept reading I had to use pointers to pass functions, I didn't know I can just pass it as is – user121615 Mar 06 '15 at 01:22
  • Please [edit] the title of your question to something meaningful. *Can't find out what's wrong with my code* has absolutely no value when it shows up in a search result for a future user of this site trying to find a solution to their problem. Your title should describe the actual problem you're having or the question you're asking, and "Can't find out what's wrong* does neither. Thanks. – Ken White Mar 06 '15 at 01:29
  • @user121615: Like arrays, functions convert to pointers if you pass them to a function. It's one of the language's funny little quirks. – Mike Seymour Mar 06 '15 at 01:29
  • I changed it Ken White. That makes sense; it can probably help some other fellow somewhere along the way. @Mike, the thing is that everyone has told me it has to be done with pointers. Maybe that's something that used to have to be done or something. Thanks for all the help guys! – user121615 Mar 06 '15 at 03:30
  • @user121615: Yes, it is done with pointers. In your code, `f` is a function pointer (but probably not to the type of function you want; it requires a function returning a `double&` reference, where you probably want to return a `double` value). You don't need any weird syntax to pass a pointer as a function argument; just name it, as you would a variable of any other type. – Mike Seymour Mar 06 '15 at 13:57

1 Answers1

3

To pass the function pointer as a function argument, just name it as you would any other variable used as a function argument:

bisection(start,end,tol,f);

Note that your parameter type is a pointer to a function returning a reference, not a reference to a function. I'm guessing that you actually want a pointer to a function returning a double by value; if that's the case, then change it to

double (*f)(double)

or just

double f(double)

which, as a function parameter, will be interpreted as a function pointer type.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644