pass by reference with function prototype?

May 22, 2014 at 6:48am
Write your question here.
I am new with pointer, and i don't really understand how it works in function prototype, hen i do this in the function prototype and the function, I can pass the value by reference:
 
void swap(int*,int*);


but okay with both
1
2
3
4
5
void swap(int a, int b){
     int temp=a;
     a=b;
     b=temp;
}


and

1
2
3
4
5
void swap(int& a, int& b){
     int temp=a;
     a=b;
     b=temp;
}


which one is the correct one?Why and how does this work??




May 22, 2014 at 6:56am
I am new with pointer, and i don't really understand how it works in function prototype, hen i do this in the function prototype and the function, I can pass the value by reference:

void swap(int*,int*);


There is no "passing a value by reference" here. More accurately, you are passing the address of two variables by value in the form of pointers.

which one is the correct one?Why and how does this work??

The latter. Look up "pass by value" and "pass by reference."
May 22, 2014 at 7:05am
Okay. But why that function with
1
2
3
4
5
void swap(int a, int b){
     int temp=a;
     a=b;
     b=temp;
}

also work too?
May 22, 2014 at 7:12am
closed account (z05DSL3A)
No; temp, a, and b are all local variables and will have no effect on the variables used as arguments when calling the function.
May 22, 2014 at 7:18am
i mean that :
1
2
3
4
5
void swap(int a, int b){
     int temp=a;
     a=b;
     b=temp;
}

would work too if I am using this as function prototype:
 
void swap(int*,int*);


Why?

Also I know the following function alone can change the value of a and b, but how does it work with the prototype too?
1
2
3
4
5
void swap(int& a, int& b){
     int temp=a;
     a=b;
     b=temp;
}


How does that work?
Last edited on May 22, 2014 at 7:20am
May 22, 2014 at 7:24am
1
2
3
4
5
void swap(int a, int b){
     int temp=a;
     a=b;
     b=temp;
}


would work too if I am using this as function prototype:

void swap(int*,int*);


No, it wouldn't.
May 22, 2014 at 7:33am
Really? Maybe my compiler fix that for me then?
So i should stick to this?
 
int& a, int& b


Also, I don't understand how this works?
in the prototype i used int*, but in the function I used int&, so how does this work?
May 22, 2014 at 7:43am
The prototype must match the definition with regards to the type(s) of parameters. If the prototype doesn't match the definition, it refers to a different function.
Last edited on May 22, 2014 at 7:44am
May 22, 2014 at 7:54am
In this function the value of a and b will change, but in the main function the value will not change, because a and b are local variables, so they can't affect the variable of main.
1
2
3
4
5
6
7
8
void swap(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
}



in this function a and b are like another name to the variables of main function. they have the same address, that's why the value can change.
1
2
3
4
5
6
void swap2(int &a, int &b)
{
    int temp = a;
    a = b;
    b = temp;
}



in this function a and b are pointing to the variable of main function, so the value will change.
1
2
3
4
5
6
void swap3(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
Last edited on May 22, 2014 at 7:57am
May 22, 2014 at 8:01am
Thanks : )
Topic archived. No new replies allowed.