Retaining the form of a function and reusing it without calling again

In the code given below, I have two functions fnc1, which is a parameter dependent function, and fnc2, which is independent of any parameters. A third function fnc12 uses this two function to do some mathematical operation. The problem is that: I want the form of function fnc2 to be retained in memory since it is parameter independent so that I need not call it again and again for different parameters of fnc1.

#include <iostream>
#include "TROOT.h"
#include "TSystem.h"
#include "TMath.h"
#include "TF1.h"
using namespace std; 

TF1 *f1, *f2, *f3, *fn23;

Double_t fnc1(Double_t *x,Double_t *par){
	return par[0]*x[0];

Double_t fnc2(Double_t *x,Double_t *par){
	Double_t pp=4*pow(x[0],3);
	return pp;

Double_t fnc3(Double_t *x,Double_t *par){
	Double_t pp=f2->Derivative(x[0]);
	return pp;

Double_t fnc12(Double_t *x,Double_t *par){
	return f1->Eval(x[0]) * f2->Derivative(x[0]);

void t3(){

	f1 = new TF1 ("fn2",fnc1,0,10,2);
	f2 = new TF1 ("fn2",fnc2,0,10,0);
	fn12 = new TF1("fn12", fnc12, 0, 10., 0.);
	string sss;
	sss = f2->GetExpFormula( "p" );//not working
	cout<<"sss = "<<sss<<endl;
	for(int i=0;i<10;i++){
	Double_t result=fn12->Integral(0,1);
	cout<<"i = "<<i<<" result = "<<result<<endl;


Thanks in advance


#include <limits>

Double_t fnc12(Double_t *x, Double_t */*par*/) {
  static double X = std::numeric_limits<double>::quiet_NaN(); // quiet_NaN() or max()
  static double V = 0.;
  if (X != x[0]) {
    X = x[0];
    V = f2->Derivative(X);
  return (V * f1->Eval(X));	

Thank you very much for your reply and solving my problem.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.