pass by reference

hi. i'm having some difficulty grasping this!! can you provide an example, or two, which explain the following: the pass by reference method copies the address of an argument into the parameter(what is the argument, and what is the parameter??) inside the subroutine, this address is used to access the actual argument specified in the call. This means that changes made to the parameter will affect te argument used to call the subroutine. it would be helpfull if you can explain which is the calling function, and which is the called function! please be clear so as to not create more questions. thanks.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

void increment_by_five(unsigned& num)
{
    num += 5 ;
}

int main()
{
     unsigned n = 0 ;

     while ( n < 25 )
     {
          std::cout << n << '\n' ;
          increment_by_five(n) ;
     }
}
0
5
10
15
20


increment_by_five takes an unsigned int by reference. The reference is named num. Any changes to num within increment_by_five will result in a change to the referenced object, as you can see from the output.

It is useful to think of a reference as simply an alias, or another name, for the referred object -- that captures the behavior of a reference quite well.
it comes to you really slowly, you dont realise you got it when you have here is a funcion that should pass by reference
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

void pass_by_reference (int &some_int,int &some_other_int)
{


some_int+some_other_int;
}


int main ()
{

int num1;

int num2;

cout <<num1<< num2;

pass_by_reference (num1, num2)

cout << num1<<num2;



}






hopefully some better non so beginner programmer will confirm my code or more likley say its off, but this is what i remember, the parenthesis tell the function exactly what to put in them, theres no return because two ints get functionified, im pretty sure thats the correct term too.
Last edited on
closed account (zb0S216C)
@cire & devonrevenge: -1 for passing built-in types by reference; it's less efficient than passing them by value. For built-in types you should pass the actual parameter by value and then return the new value. You should only pass types by reference if they consist of two or more data members.

Wazzak
Last edited on
@Framework: -1 for pointless criticism. I'm not going to confuse a beginner by introducing needless complexity.
@devonrevenge -1 for not noticing how clever a tag code to joy is

i love peggle, they play that when you win XD
I'll put it simply, so it's easy to understand.
Every variable you work with, every function, all data is stored somewhere in memory for the processor to easily manipulate it.
When you're dealing with references, you're working with a variable that, simply put, shares the same memory as another of the same type. A reference is basically a constant pointer (not a pointer to a constant, which is different).
Look into pointers and memory management to understand this to work with memory in the raw. It's dangerous but extremely powerful and efficient.
Last edited on
closed account (zb0S216C)
cire wrote:
"I'm not going to confuse a beginner by introducing needless complexity."

That "needless complexity", as you put it, is something every programmer should know. Introducing it early increases the possibility of the OP avoiding such code in the future. Besides, it's not complex at all.

Nexius wrote:
"When you're dealing with references, you're working with a variable that, simply put, shares the same memory as another of the same type. A reference is basically a constant pointer (not a pointer to a constant, which is different)."
-1 You're treading on thin ice here. A reference might (with emphasis) allocate memory; whether a reference allocates memory depends on how the implementation implements the reference. Also, a reference is not always implemented with a pointer, and by stating that a reference is a pointer is simply misleading.

Wazzak
Last edited on
That "needless complexity", as you put it, is something every programmer should know. Introducing it early increases the possibility of the OP avoiding such code in the future. Besides, it's not complex at all.


There are many things every programmer should know. It isn't necessary to explain each of them for every simple example. If you feel you have something to add to an explanation, feel free to add it.

But, shove your -1's.
@Framework: -1 for pointless criticism. I'm not going to confuse a beginner by introducing needless complexity.

@ cire: +1 for the burn!

That "needless complexity", as you put it, is something every programmer should know. Introducing it early increases the possibility of the OP avoiding such code in the future. Besides, it's not complex at all.

This thread is about clearing up how references work.

(Catfish2, -1 for being off-topic.)
codetojoy wrote:
the pass by reference method copies the address of an argument into the parameter

Do not confuse the ampersand & operator, which takes something's address, with the ampersand used in declaring a reference.
1
2
3
4
5
6
7
8
int i;
int &ri = i; // ri is a reference (an "alias") to i

int *pi = &i; // & is an operator that takes i's address

void function(int &ri) // ri is a reference, & is not an operator
{
}
yes lets not be throwing points around thats what they do at dream in code, theres something wanky about it all.

check this, this is interesting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main()
{
char j= 'j';
char *pointstoj;
pointstoj = &j;

cout << *pointstoj ;

 char **pontypointer;

pontypointer=&pointstoj;

cout << **pontypointer<<endl;

char ***potty;

potty=&pontypointer;

cout << ***potty<<endl;

}


Topic archived. No new replies allowed.