// function ask for void function with int param
void func ( void (*f)(int) );
// void function with int param :)
void print ( int x ) {
printf("%d
", x);
}
// pass print function to func
func( print )
// C++ program to pass function as a
// pointer to any function
#include <iostream>
using namespace std;
// Function that add two numbers
int add(int x, int y)
{
return x + y;
}
// Function that multiplies two
// numbers
int multiply(int x, int y)
{
return x * y;
}
// Function that takes a pointer
// to a function
int invoke(int x, int y,
int (*func)(int, int))
{
return func(x, y);
}
// Driver Code
int main()
{
// Pass pointers to add & multiply
// function as required
cout << "Addition of 20 and 10 is ";
cout << invoke(20, 10, &add)
<< '
';
cout << "Multiplication of 20"
<< " and 10 is ";
cout << invoke(20, 10, &multiply)
<< '
';
return 0;
}
// program to print a text
#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main() {
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;
}
// program to print a text
#include <iostream>
using namespace std;
// display a number
void displayNum(int n1, float n2) {
cout << "The int number is " << n1;
cout << "The double number is " << n2;
}
int main() {
int num1 = 5;
double num2 = 5.5;
// calling the function
displayNum(num1, num2);
return 0;
}