in your case, you are sending the address of a variable to a function, thus you are making direct changes in a function to the variables being sent
in other words, when you send x to a &a , you are actually sending the address location of x, that way the functionc an make direct changes to the value of x without having to return a value of any kind.
Thanks...means that if use void duplicate (int a, int b, int c), the function need to return a value is it? And if use void duplicate (int &a, int &b, int &c), the function no need returnn any value? Am I correct?
How if I want to make the 2nd code to use return value? How to modify it? TQ...
Not necessarily... There are two types of functions
Pass by value
and
Pass by reference
in your case you are doing pass by reference, meaning you are making direct changes to a value
if you didn't use the &, you are doing pass by value, in other words you are making a copy of the value
return type is dependent on the function.
For instance if you wanted to do the addition of the 3 values being sent, you return the sum back to the function.
1 2 3
int sum(x, y, z){
return x+y+z;
}
if you wanted to make changes to the values being sent, use what you did
if you wanted to copy the values to do something different like check if the numbers are valid you could do something like
1 2 3 4 5
bool test(x, y, z){
if ( x < 0 || y < 0 || z < 0)
returnfalse;
elsereturntrue;
}
or in your case, you wanted to get the squared value of each value.. you could do
1 2 3 4 5 6
void duplicate (int a, int b, int c)
{
a*=a; // a^2
b*=b; //b^2
c*=c; // c^2
}