Help to correct my mistake

Nov 11, 2014 at 12:16pm
Dear Friends,
Below I wrote one part of a code. In this part I defined a function get_particle_velocity which calculates u[][] according to the other parameters.
Now if I want to use the get_particle_velocity in my code for example in a for loop can I write:
get_particle_velocity(n, x, u, fluid_u); or I should write get_particle_velocity(&n, &x, &u, &fluid_u)?
If not what should I write?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    void get_particle_velocity(int n, double **x, double **u, double ***fluid_u, double **geometry){
	int px, py;
    int j;

	px=int(x[n][0]);
	py=int(x[n][1]);

	for (j=0; j<2; j++){
		if(geometry [px][py]==0 || particle_status[n]==0)
			u[n][j] = fluid_u[px][py][j];
		else
			u[n][j] = 0;
	}
}

Nov 11, 2014 at 7:45pm
Your function receives an int, a double, a double, a double, and a double in that order. If you were to call it, it would look something like this:

get_particle_velocity(n, x, u, fluid_u, geometry);

All those variables in the parentheses are ones that you initialize outside of the function, and the function wont change any of them since they are passed by value, if you wanted to change those variables, you would need to pass by reference, which uses "&". In this case when you write the function you would need to add a & next to each of the variables in the header that you want to pass by reference. So your header for your function would look like this:

1
2
3
4
void get_particle_velocity(int &n, double &**x, double &**u, double &**fluid_u, double &**geometry)
{
...
}


and your call would look the same, you would just need to use the same names in the function header and when you initialize your variables.

Also when you change something about the parameters of a function, don't forget to change it in the function prototype.

Hope this helps!
Last edited on Nov 11, 2014 at 7:46pm
Topic archived. No new replies allowed.