If I understand you correctly then following code should help you.
// Define your functions here
// Your first function
double func1(double x)
{
double y = sqrt(x);
return y;
}
// Your second function
double func2(double x)
{
double y = x^2;
return y;
}
// Define pointer for function
typedef double (*PFNSUM)(double);
// This is the Sum function - uses function pointer to call appropriate function
// The Sum computed is a simple sum of y values at each x - you can replace with your algorithm as needed
double Sum(PFNSUM pfn, double xmin, double xmax, double xstep)
{
double sum = 0;
for(double x = xmin; x <= xmax; x += xstep)
sum += pfn(x);
return sum;
}
// This function makes it accessible from LabTalk
// You can go to script window and type:
// mysum = LTSum(func1, 0, 5, 1)
// and the LT variable mysum will have your sum value
double LTSum(string strFunc, double xmin, double xmax, double xstep)
{
PFNSUM pfn;
// Check name passed from LabTalk and assign pointer accordingly
if( 0 == strFunc.CompareNoCase("func1") )
pfn = func1;
else if( 0 == strFunc.CompareNoCase("func2") )
pfn = func2;
else
return NANUM; // if no match on function name, return missing value
return Sum(pfn, xmin, xmax, xstep); // else call Sum function to compute
}
// This shows how you will call the Sum function from Origin C
void dd()
{
// Call Sum by directly specifying function name as argument
double y = Sum(func1, 1, 2, 1);
}
Edited by - eparent on 05/29/2003 10:47:48 AM