Hello! What you usually do here in C is to pass an array by pointer, plus an additional parameter for the size, just like it already happens for the Legendre function in your script. The disang function would then look like this:

double disang(double x, int nPar, double *a) {
double W = 1.0;
for (int j = 1; j <= nPar; j++) {
W += a[j - 1] * Legendre(j, x);
}
return W;
}

If you can switch from C to C++, you might consider std::vector

#include <vector>
double disang(double x, const std::vector<double>& a) {
double W = 1.0;
int j = 1;
for (auto v : a) {
W += v * Legendre(j, x);
j++;
}
return W;
}

Also note that arrays in C / C++ are zero based and not 1 based. So starting your loop at 1 and ending at nPar means that your array must have nPar+1 elements, and you’re ignoring the first element. In my C++ version I have left j starting at 1 so that Legendre is called with the same start value for the loop.

Hello @jonas and @ctacke.
I realized that it is not enough changing the disang function to use the macro with a general number of fitting parameters. That’s because the fit function is

double fitfun(double *x, double *par) {
double xx = x[0];
double Sum = 0;
for (int k=1; k<Npar; k++) {
Sum = Sum + par[k]*Legendre(k, xx);
}
return par[0]*(1 + Sum);
}

and called as

fit[n] = new TF1("fit", fitfun, -1, 1, 3);

being Npar the gloabal variable

const int Npar = 3;

Then I’ve also pass the Npar value (that is now given by command line) to the fitfun function.
Do you know how to do please?
If I try to pass it in this way:

double fitfun(double *x, double *par, int Npar) {

fit[n] = new TF1("fit", fitfun(Npar), -1, 1, 3);

I get this error

error: no matching function for call to 'fitfun'
fit[n] = new TF1("fit", fitfun(Npar), -1, 1, 3);
^~~~~~
note: candidate function not viable: requires 3 arguments, but 1 was provided
double fitfun(double *x, double *par, int Npar) {

because the fitfun already had double*x,double *par