We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,692 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Passing Functions as Parameters

I am trying to experiment with C++ to make the command prompt function like MatLab or Octave.

This is a practice file I've been doing so I could pass functions as parameters to other functions.

My problem is with this function call found in line 36

compute(gset[function-1], input);

This is supposed to be correct according to what I know and according to another program that uses this similar snippet with the difference being the implementation of the function that I am passing a function to is in a separate header file.

So far I only have one compilation error and if somebody can help me with this, I'd truly be grateful.

#include <iostream>
//#include <math.h>
//#include <iomanip>
//#include <stdio.h>
using namespace std;
   
double f1(double x)
{
    return(2*x);
}

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

double compute(double f(double x), double i)
{
       double answer;
       answer = f(i);
       return (answer);       
}

int main()
{
    double input, function, ans;
    
    double (*gset[])(double)={f1,f2};
    Input:
    
    cout << "Input Please: ";
    cin >> input;
    cout << "Function Please: ";
    cin >> function;
    
    ans = compute(gset[function-1], input);
    cout << ans;
    
    goto Input;
    
    
}
2
Contributors
3
Replies
4 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
CombatJ
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

So.. what's the compilation error message? At what line?

I would probably try to pass in a function pointer instead, since "gset" is an array of function pointers, maybe try to define the "compute" function as:

double compute(double (*f)(double x), double i)

The rest can remain unchanged.

Also, as a matter of habit, you should avoid using a goto statement, because it is generally considered bad practice and off-putting for any programmer who might look at the code. Especially in this case, it is easy to avoid it with:

while(true) {
 
        cout << "Input Please: ";
        cin >> input;
        cout << "Function Please: ";
        cin >> function;
 
        ans = compute(gset[function-1], input);
        cout << ans;
 
    };

Also notice how the indentation makes it clearer that this is a loop. Even if you use goto-statements, you should indent if you can.

mike_2000_17
21st Century Viking
Moderator
3,135 posts since Jul 2010
Reputation Points: 2,050
Solved Threads: 625
Skill Endorsements: 41

Thank you, mike, for your reply and your advice. :D

Using the code that I posted, the compiler error is from line 36:

invalid types 'double(*[2](double)[double]' for array subscript

From what I know, since gset is an array I can simply use the function at an index function - 1.

By making the function call:

compute(gset[function-1], input);

I hoped that gset[function-1] could call the function i needed from the array.

I tried defining function "compute" with this

double compute(double (*f)(double x), double i)

but more errors arose

I'm not sure why there are errors in my original code.

CombatJ
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

So.. what's the compilation error message? At what line?

I would probably try to pass in a function pointer instead, since "gset" is an array of function pointers, maybe try to define the "compute" function as:

double compute(double (*f)(double x), double i)

The rest can remain unchanged.

Also, as a matter of habit, you should avoid using a goto statement, because it is generally considered bad practice and off-putting for any programmer who might look at the code. Especially in this case, it is easy to avoid it with:

while(true) {
 
        cout << "Input Please: ";
        cin >> input;
        cout << "Function Please: ";
        cin >> function;
 
        ans = compute(gset[function-1], input);
        cout << ans;
 
    };

Also notice how the indentation makes it clearer that this is a loop. Even if you use goto-statements, you should indent if you can.

No worries. Got the code fixed already. I had the foolish mistake of using type double for a variable that's going to be used for an index on line 26 from my original code. I should've used type int for function. Thanks again. :)

CombatJ
Newbie Poster
3 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0655 seconds using 2.7MB