int a, int b, int c/int &a, int &b, int &c??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio>

void duplicate (int &a, int &b, int &c)
{
	a*=2;
   b*=2;
   c*=2;
}

int main()
{
	int x=1,y=3,z=7;
   duplicate (x,y,z);
   cout<<"x="<<x<<", y="<<y<<", z="<<z;
   getch();
   return 0;
}


Can anyone tell me what's the different between code about which have "&" and code below that doesn't have "&"? Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio>

void duplicate (int a, int b, int c)
{
	a*=2;
   b*=2;
   c*=2;
}

int main()
{
	int x=1,y=3,z=7;
   duplicate (x,y,z);
   cout<<"x="<<x<<", y="<<y<<", z="<<z;
   getch();
   return 0;
}
& represents the address of the variable.

* defines a pointer

pointers point to addresses

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.
Last edited on
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...
Another question is, "void" use when no need return value where as "int" use whenever we need to return value, is 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) 
  return false;
else return true;
}


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
}


then all the values you send would be squared
to answer your next question..

the type before the function is the return type..

int returns an int,

bool returns a boolean value (0 or 1) or (true and false)

double returns a double

etc..

void returns nothing and does not need a return type

when you get more advanced, you can return class objects and other things :)
Thanks oonej for the explanation

As u said, int need to return a value, how if I do like this, do I need to return a value? How to return the value? TQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio>

int duplicate (int &a, int &b, int &c)
{
	a*=2;
   b*=2;
   c*=2;
}

int main()
{
	int x=1,y=3,z=7;
   duplicate (x,y,z);
   cout<<"x="<<x<<", y="<<y<<", z="<<z;
   getch();
   return 0;
}



=======================================================

or in your case, you wanted to get the squared value of each value.. you could do

void duplicate (int a, int b, int c)
{
a*=a; // a^2
b*=b; //b^2
c*=c; // c^2
}

then all the values you send would be squared


Is that u mean change the code like the one below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio>

void duplicate (int a, int b, int c)
{
   a*=a; // a^2
   b*=b; //b^2
   c*=c; // c^2
}

int main()
{
	int x=1,y=3,z=7;
   duplicate (x,y,z);
   cout<<"x="<<x<<", y="<<y<<", z="<<z;
   getch();
   return 0;
}


I still can't get the squred value...anything wrong in my code? TQ...

=======================================================

1
2
3
4
5
bool test(x, y, z){
if ( x < 0 || y < 0 || z < 0) 
  return false;
else return true;
}


The terminal won't show out "false" or "true" right? How to know if the code return either "false" or "true"? TQ...
my code that you are trying to use, the squared values of the numbers

is pass by reference, you have to use the address (&) in front of the variables a b and c in the declaration
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
}


bool returns a true or false,

so if you want to use a function to check your while statements

you can't return 3 values, at least i don't know how.

while(thisfunction(x, y, z))

this would run until this function came up false
Topic archived. No new replies allowed.