In C , parameters passed to a function are typically copied by value. This means that any modifications made to the parameter within the function do not impact the original value in the caller.
In the given code, the trans function is defined as:
void trans(double x,double y,double theta,double m,double n) { m=cos(theta)*x+sin(theta)*y; n=-sin(theta)*x+cos(theta)*y; }
If you call trans from the same file as:
trans(center_x,center_y,angle,xc,yc);
the values of xc and yc will not be modified within the function. To achieve this, you have two options:
Use References in C :
Add an ampersand (&) before each parameter m and n in the function definition:
void trans(double x, double y, double theta, double& m, double& n)
In the caller, pass the variables without the ampersand:
trans(center_x, center_y, angle, xc, yc);
Use Pointers in C:
Change the function definition to accept pointers:
void trans(double x, double y, double theta, double* m, double* n)
In the caller, pass the address of the variables:
trans(center_x, center_y, angle, &xc, &yc);
By using either method, the values of xc and yc will be modified within the trans function, as they are now being passed by reference or pointer. This allows the function to directly modify the original variables in the caller.
The above is the detailed content of How Can I Modify Function Parameters and Reflect Changes in the Caller in C ?. For more information, please follow other related articles on the PHP Chinese website!