How to pass a function as a parameter in C++

2 Answers

0 votes
#include <iostream>
 
using namespace std;
 
typedef void (*function_pointer)(int args);
 
void f(int n, function_pointer show) {
   show(n);
}
 
void show(int n) {
   cout << n;
}
 
 
int main() {
   f(100, (function_pointer)show);
     
   return 0;
}
 
 
 
 
/*
run:
 
100
 
*/

 



answered Feb 2, 2020 by avibootz
0 votes
#include <iostream>
 
using namespace std;
 
void f(int n, void (*function_pointer)(int)) {
   function_pointer(n);
}
 
void show(int n) {
   cout << n;
}
 
 
int main() {
   f(100, show);
     
   return 0;
}
 
 
 
 
/*
run:
 
100
 
*/

 



answered Feb 2, 2020 by avibootz
...