3 Replies - 815 Views - Last Post: 30 March 2012 - 05:33 PM Rate Topic: -----

#1 indyarocks  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 07-March 12

Passing pointer to a function as argument to other function.

Posted 30 March 2012 - 02:51 PM

Hi,

I was reading the concept of passing pointer to a function as argument to other function. I tried to understand the example given in the book. But I feel there is a glitch. I tried to compile the program, though its compiling successfully, but results are not as expected. Here is the code
//Use of an array of pointers to functions
#include <iostream>

using namespace std;

enum BOOL {FALSE, TRUE};
void changeVal(int &, int &);
void square(int &, int &);
void cube(int & , int &);
void swap(int &, int &);
void getVal(void (*) (int &, int &),int, int);

int main()
{
    int x,y;
    BOOL fQuit = FALSE;
    void (*Func)(int &, int&);
    cout << "Enter the value of x\n";
    cin >> x;
    cout << "Enter the value of y\n";
    cin >> y;
    while(fQuit == FALSE)
    {
        int choice;
        cout << "0.Quit, 1.Change, 2.Square, 3.Cube, 4.Swap\n";
        cin >> choice;
        switch(choice)
        {
            case 1: Func = changeVal; break;
            case 2: Func = square; break;
            case 3: Func = cube; break;
            case 4: Func = swap; break;
            default: fQuit = TRUE; break;
        }
        if(fQuit)
            break;
        getVal(Func, x, y);

    }
    return 0;
}

void changeVal(int &rx, int &ry)
{
  cout << "New X :";
  cin >> rx;
  cout << "New Y :";
  cin >> ry;
}
void square(int &rx, int &ry)
{
    rx *= rx;
    ry *= ry;
}
void cube(int &rx , int &ry)
{
    rx = rx*rx*rx;
    ry = ry*ry*ry;
}
void swap(int &rx, int &ry)
{
    int temp;
    temp = rx;
    rx = ry;
    ry = temp;
}
void getVal(void (*Func) (int &, int &),int x, int y)
{
    cout << "x: " << x << " y: " << y << endl;
}

And here is the output
Enter the value of x
2
Enter the value of y
3
0.Quit, 1.Change, 2.Square, 3.Cube, 4.Swap
3
x: 2 y: 3
0.Quit, 1.Change, 2.Square, 3.Cube, 4.Swap
3
x: 2 y: 3
0.Quit, 1.Change, 2.Square, 3.Cube, 4.Swap
1
x: 2 y: 3
0.Quit, 1.Change, 2.Square, 3.Cube, 4.Swap
4
x: 2 y: 3
0.Quit, 1.Change, 2.Square, 3.Cube, 4.Swap
0

Process returned 0 (0x0)   execution time : 20.180 s
Press any key to continue.

After going through the example in the book, I felt something fishy. In line 37, getVal(Func, x, y); we have not given any parameters for Func. Also in the definition of getVal (line no 67)void getVal(void (*Func) (int &, int &),int x, int y) We have not passed any parameter for Func, then how will it initialize ? To debug, I changed this to getVal(Func(x,y), x, y); but then, compiler says
C:\CodeBlock\SpclPassFuncPointer\main.cpp||In function 'int main()':|
C:\CodeBlock\SpclPassFuncPointer\main.cpp|37|error: invalid use of void expression|
||=== Build finished: 1 errors, 0 warnings ===|



Any idea how to initialize this Func ?

Quote

Got the answer,
by defining Func(x,y) inside the definition of getVal().
void getVal(void (*Func) (int &, int &),int x, int y)
{
    cout << "x: " << x << " y: " << y << endl;
    Func(x,y);
    cout << "x: " << x << " y: " << y << endl;
}


Sorry for bothering..Thanks.:)

This post has been edited by indyarocks: 30 March 2012 - 02:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing pointer to a function as argument to other function.

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Passing pointer to a function as argument to other function.

Posted 30 March 2012 - 03:24 PM

Your solution isn't using Func for its intended purpose. A better solution is to change getVal's last 2 arguments to int&, then fix line 37 to actually use the Func pointer, and add code to line 38 to print the results:

//Use of an array of pointers to functions
#include <iostream>

using namespace std;

enum BOOL {FALSE, TRUE};
void changeVal(int &, int &);
void square(int &, int &);
void cube(int & , int &);
void swap(int &, int &);
void getVal(void (*) (int &, int &),int&, int&);

int main()
{
    int x,y;
    BOOL fQuit = FALSE;
    void (*Func)(int &, int&);
    cout << "Enter the value of x\n";
    cin >> x;
    cout << "Enter the value of y\n";
    cin >> y;
    while(fQuit == FALSE)
    {
        int choice;
        cout << "0.Quit, 1.Change, 2.Square, 3.Cube, 4.Swap\n";
        cin >> choice;
        switch(choice)
        {
            case 1: Func = changeVal; break;
            case 2: Func = square; break;
            case 3: Func = cube; break;
            case 4: Func = swap; break;
            default: fQuit = TRUE; break;
        }
        if(fQuit)
            break;
        Func(x, y);
        cout << "x = " << x << "; y = " << y << endl;

    }
    return 0;
}

void changeVal(int &rx, int &ry)
{
  cout << "New X :";
  cin >> rx;
  cout << "New Y :";
  cin >> ry;
}
void square(int &rx, int &ry)
{
    rx *= rx;
    ry *= ry;
}
void cube(int &rx , int &ry)
{
    rx = rx*rx*rx;
    ry = ry*ry*ry;
}
void swap(int &rx, int &ry)
{
    int temp;
    temp = rx;
    rx = ry;
    ry = temp;
}
void getVal(void (*Func) (int &, int &),int& x, int& y)
{
    cout << "x: " << x << " y: " << y << endl;
}


Was This Post Helpful? 1
  • +
  • -

#3 indyarocks  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 07-March 12

Re: Passing pointer to a function as argument to other function.

Posted 30 March 2012 - 03:58 PM

View Postr.stiltskin, on 30 March 2012 - 03:24 PM, said:

Your solution isn't using Func for its intended purpose. A better solution is to change getVal's last 2 arguments to int&, then fix line 37 to actually use the Func pointer, and add code to line 38 to print the results:
void getVal(void (*) (int &, int &),int&, int&);

Thanks Stilt..:) Your answer was useful. I think you recommended to pass by reference because of swap function. Otherwise Passing by value will do. Suppose we just want to calculate the square or cube of two numbers without changing them,(Though, yea , swap wont work here if I pass by value). Did you mean the same ?

This post has been edited by indyarocks: 30 March 2012 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Passing pointer to a function as argument to other function.

Posted 30 March 2012 - 05:33 PM

Not only swap. All four of those functions are taking references are arguments, so they are clearly intended to change the values that the variables that are passed have in main. If that wasn't intended, the function argument lists should have been (int, int).

I merely made the function pointer consistent with the functions it was meant to point to.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1