uopspop

Untitled

Dec 29th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int addInt(int n, int m){
  5.     return n + m;
  6. }
  7.  
  8. int timeInt(int n, int m){
  9.     return n * m;
  10. }
  11.  
  12. int calTwoNumbers(int(*functionPtr)(int,int),int n, int m){
  13.     return functionPtr(n,m);
  14. }
  15.  
  16. int main(){
  17.  
  18. /************** function pointer - function as parameters *****************/
  19.     int sum = 0;
  20.  
  21.     // declare function pointer
  22.     int (*functionPtr)(int,int);
  23.  
  24.     // pass function as parameters:
  25.     sum = calTwoNumbers(addInt,2,3);
  26.     printf("%d\n",sum); // 2 + 3 = 5
  27.     sum = calTwoNumbers(timeInt,2,3);
  28.     printf("%d\n",sum); // 2 * 3 = 6
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment