English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Why do some functions have & in them?
example :
void aFunction ( int a, int b, int& c)
???

2007-09-25 06:59:40 · 3 answers · asked by ashcatash 5 in Computers & Internet Programming & Design

3 answers

Variables a and b are passed by value, that is to say that a copy of the variable values are passed to the function. So if aFunction() includes the command a=5; only the local copy of the value inside the function is modified, the original variable passed to the function remains unchanged.

Variable c is passed by reference, that is to say that a pointer to the original c variable is passed to aFunction() such that the command c=5; changes not only the copy of the variable inside the function, it also changes the value in the original calling function.

If you are familier with pointers, it's a lot like
void aFunction( int a, int b, int* c )
except with this type of function, you have to say *c=5 to dereference the pointer. When passed by reference with &, the complier automatically dereferences the pointer for you.

2007-09-25 07:13:53 · answer #1 · answered by HooKooDooKu 6 · 2 0

C++ is a very easy and inter\esting language..
int a,b,c are the general things.
when we make the program of solving a math question.
then we use these numbers like algebra.
and when the program executes then computer asks from us the values of these integers.
then program runs according to these numbers.

2007-09-25 14:39:10 · answer #2 · answered by BADAR M 2 · 0 1

study call by reference and call by value in functions chapter
it will clear ur confusion

2007-09-26 01:50:23 · answer #3 · answered by i_am_the_next_best_one 5 · 0 1

fedest.com, questions and answers