external data available inside function

Dec 30, 2013 at 9:20pm
For calculations inside a function I need data/variables from outside this function (not passed in as parameters) - how can one make it accessible inside the function? Something corresponding to COMMON in Fortran.
Dec 30, 2013 at 9:30pm
Can you post your code so that everybody understands what are your problems.
Dec 30, 2013 at 9:32pm
You can declare your variables outside the function (as global variable)
1
2
3
4
5
6
7
#include<iostream>
using namespace std;
int a=5;
int main()
{
    cout<<a<<endl;
}
Dec 30, 2013 at 9:39pm
The question is: how can the values of variables iNum, x_point, y_point, error_y be available in that routine?



void calc_chi_square(Int_t &npar, Double_t *gin, Double_t &f, Double_t *par, Int_t iflag)
{

double chisq = 0;

const int iNum;
Float_t x_point[3],y_point[3],error_y[3];


for (int i=0;i<iNum; i++) {
// chi square is the quadratic sum of the distance from the point to the function weighted by its error
double delta = (y_point[i] - fit_function(x_point[i],par))/error_y[i];
chisq += delta*delta;
}
f = chisq;
return;
}

Dec 31, 2013 at 5:33pm
they cant by accessed out side of [void calc_chi_square{ acces ONLY here}]

when calc_chi_square ends the variables isnt exist any longer
you need to declare them somewhere outside where they exsis at the time you wana use them and are accessible by you rutine from which you wana acces them
Topic archived. No new replies allowed.