Thread: Passing a function name as a variable to execute it?

  1. #1
    Registered User
    Join Date
    Jan 2002

    Question Passing a function name as a variable to execute it?

    Probably more C than C++, but I'm using Borland Builder (v3), so I have that available to me.

    Iím writing an application that reads a script file. The script file might look something like this.

    5~add~1 2
    10~subtract~5 3
    15~multiply~3 4

    It just breaks down as a ď~Ē delimited file. LineNumber~function~parameters.

    Now, I can parse it out just fine and get the line numbers into int variables and the functions into char* or AnsiString or whatever variables and get the parameters parsed out ok. What I need to do now is call the functions by those names.

    i.e. I have functions

    int add(int x, int y)

    return (x+y);

    int subtract(int x, int y)
    return (x-y);

    int multiply(int x, int y)

    int main()
    char function[20];
    int x;
    int y;
    GetScriptLine(function, &x, &y);

    // function now contains the char array ďaddĒ and x==1 and y==2
    // can I call a function something like this

    Execute(function, x, y);

    /*where Execute() runs the add function in this case and the subtract function on the next script line and multiplyÖ without a big switch statement or a group of if's? Iíve been reading up on pointers to functions, but I havenít found any examples of
    doing anything quite like this. Even something like the JavaScript::eval() function would be fine.*/
    return 0;

    Of course the program is not quite as simple as common math functions, and will have many functions to call (that's why I don't want to use if's) but if I can accomplish this, the translation will be a snap. Any Ideas?

    It's not a problem to have to have the parameters all the same either. I have a facility for doing that if I need it. i.e. Execute(function, &DataList); where DataList is simply a list of strings (I'll do the conversion in the add(), subtract(), multiply()... functions because in the real app, the functions will take considerably different arguments.

    Happy New Year by the way.

    Thanks for any help,


  2. #2
    Super Moderator
    Join Date
    Sep 2001
    You can use a pointer to a function. Declaration syntax:

    function_return_type (*pointer_name)(parameter_types).

    That is all I know - I'm not 100% sure how you would then access the function, but I know to assign the function to the pointer you would use the above syntax, then :

    = &function_name();. I'm not sure whether or not you need the ()'s, but this is a start.

    You have 0 posts, so if you're a newbie, you'll have to learn what pointers are.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Here is a possible solution. Use a STL map. The key will be the STL string that has the function name, and it will hold the function pointers.
    So once at the start of your program you add all the functions to the map, which i called lookuptable, then when you read in the string simply search the look up table using the [] operator.
    If you want more info on a map check out

    On a side note, when i compile this code it runs fine, but i get about 98 warnings dealing with STL crap. I know there is some option to turn the warning off. Back in CS2 we did
    #pragma SomeOptionHere
    and it had a 4 digit number in it, i believe. Anyone know what im talking about, or how to remove these stupid STL warnings?

    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    int (*fPtr)(int, int);
    int Add(int a,int b);
    int Sub(int a,int b);
    //key is a string
    //data type is a pointer to a function that returns 1 int, and takes 2 ints has parameters
    map<string, int(*)(int,int)> LookUpTable;
    int main() 
     //place all functions into look up table
    	LookUpTable["Add"] = Add;
    	LookUpTable["Sub"] = Sub;
      //function that we want
    	string function = "Add";
     //find it in the look up table
    	fPtr = LookUpTable["Add"];
      //call the add function from the function pointer we got from the look up table
    	cout << fPtr(1,2) << endl; //this will print out 3 (1+2=3)
      //call the sub function directly from the look up table
    	cout << LookUpTable["Sub"](2,1) << endl; //this will print out 1 (2-1=1)
      //the look up table will return null if the search key cannot be found
    	fPtr = LookUpTable["Not In Here"];
    	if( fPtr == NULL )
    		cout << "its null";
    	return 0;
    int Add(int a,int b)
    	return a+b;
    int Sub(int a,int b)
    	return a-b;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM