Friday, December 23, 2016

Passing a 'Function' as a argument to another Function-C++

#include<iostream>
using namespace std;

int sum(int a,int b)
{
   return a+b;
}
void f2(int (*f)(int,int),int a,int b) //'*f' is a POINTER TO A FUNCTION whose RETURN TYPE is                                                           //'int' and arg type (int,int)  . Here, '*f' is FORMAL PARAMETER
{                                                                
  cout<<sum(a,b)<<endl;
}
int main()
{   int (*p1)(int,int)=&sum;          //Here, '*pf1' is ACTUAL PARAMETER
    f2(sum,10,20);                         //NAME OF A FUNCTION IS ITSELF A POINTER TO ITSELF
    f2(p1,20,79);
    return 0;

}

No comments:

Post a Comment

Henry's law constant for CO 2 ​ in water is 1.67×10 ∘ Pa at 298 K . Calculate the quantity of CO 2 ​ in 500 mL , of soda water when packed under 2.5 atm CO 2 ​ pressure at 298 K .

  Explanation To calculate the amount of CO 2 ​ dissolved, we use Henry's Law: P = k H ​ × x _ C O 2 ​ where:\ P = partial pressure of...