Fespace Vh as the input argument of a function

Is it possible to set the fespace itself as an input argument of a self-defined func function? My problem is related to the node id of the fespace, and a simplified example is as follows:

mesh Th = square(2, 2);
fespace Wh(Th, P2);

for (int k = 0; k < Th.nt; k++) {
    for (int i = 0; i < 6; i++) {
        int j = Wh(k, i);
        cout << j << " ";
    }
    cout << endl;
}

Is it possible to define a function and use the fespace Wh as an input argument? A test is as follows, but an error called syntax error before token fespace occurs.

func int test(fespace Wh) {
    int j = Wh(0, 0);
    return j;
}

If it is not possible to implement such function, should the mesh Th be the input argument, or just define the mesh and fespace within the func function?

You can use a macro, since it uses literal (string) arguments without any check of their nature.

NewMacro funcofmesh(Whh)// macro with formal fespace as argument
func int Whh#test() {
    int j = Whh(0, 0);
    return j;
}
EndMacro


mesh Th = square(2, 2);
fespace Wh(Th, P2);

funcofmesh(Wh);// this defines the function "Whtest"
int jres=Whtest();
cout << "jres=" << jres << endl;

Another possibility is

NewMacro funcofmesh(Whh,res)// macro with formal fespace as argument
 res = Whh(0, 0);
EndMacro


mesh Th = square(2, 2);
fespace Wh(Th, P2);

int jres;
funcofmesh(Wh,jres);
cout << "jres=" << jres << endl;
1 Like

Dear Dr. Bouchut,

Thanks for your reply, and using the macro is a good solution.