Newbie Function Question.?

JohnLenning

New member
Joined
May 1, 2011
Messages
0
Reaction score
0
Points
0
I don't understand what the double ints are for like in:
int operation (int x, int y, int (*functocall)(int,int))

What does this mean?
int (*minus)(int,int) = subtraction;


// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}

int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;

m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
 
int operation (int x, int y, int (*functocall)(int,int))
This means that 'operation' is a function that returns an integer (that's the 'int operation (' part). It takes as parameters two integers, called 'x' and 'y' (that's the 'int x, int y' part), and a pointer to a function called 'functocall' which returns an integer and takes two integers (that's the 'int (*functocall)(int,int)' part).

int (*minus)(int,int) = subtraction;
This creates a pointer called 'minus' (that's the '*minus' part). It points to a function takes two integers as parameters and returns one (that's the 'int' and '(int,int)' parts). The pointer initially points to the function named 'subtraction' (that's the '=subtraction' part).

Hope that helps.
 
Back
Top