Home > Backend Development > C++ > How Do Parameter Modifications Inside a Function Affect the Calling Function in C and C ?

How Do Parameter Modifications Inside a Function Affect the Calling Function in C and C ?

Barbara Streisand
Release: 2024-12-18 22:57:18
Original
524 people have browsed it

How Do Parameter Modifications Inside a Function Affect the Calling Function in C and C  ?

Parameter Modification within Functions: Impact on the Caller

When modifying a parameter within a function, it's crucial to understand its effect on the caller. In the presented code snippet:

<br>void trans(double x,double y,double theta,double m,double n)<br>{</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">m=cos(theta)*x+sin(theta)*y;
n=-sin(theta)*x+cos(theta)*y;
Copy after login
Copy after login

}

calling this function with trans(center_x,center_y,angle,xc,yc) doesn't directly modify the values of xc and yc. This occurs because C passes function parameters by value, meaning the function receives a copy of the variables.

To address this, you have two options:

1. In C :

Use references to pass parameters by reference, modifying the original variables within the function:

<br>void trans(double x, double y, double theta, double& m, double& n)<br>{</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">m=cos(theta)*x+sin(theta)*y;
n=-sin(theta)*x+cos(theta)*y;
Copy after login
Copy after login

}

2. In C:

Pass parameters by explicitly passing their addresses using pointers:

<br>void trans(double x, double y, double theta, double<em> m, double</em> n)<br>{</p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">*m=cos(theta)*x+sin(theta)*y;
*n=-sin(theta)*x+cos(theta)*y;
Copy after login

}

With these modifications, calling trans(center_x,center_y,angle,xc,yc) will directly update the values of xc and yc. If this behavior is desired, then using references or pointers is necessary to achieve the desired effect.

The above is the detailed content of How Do Parameter Modifications Inside a Function Affect the Calling Function in C and C ?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Previous article:How Does XORing with 32 Efficiently Convert Case in ASCII? Next article:How to Safely Remove Elements from a std::list While Iterating?
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template