pass by reference and pass and return

What is the difference between a function passing by reference and a function passing the variable and return it to the original variable. It seems to do the same?

This is the example code i was testing it on:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;
void passByValue(int var);
void passByRef(int *var);
int passAndReturn(int var);

int main(){
	int a = 13;
	int b = 13;
	
	cout << a << endl;
	cout << b << endl <<endl;
	
	passByValue(a);
	
	passByRef(&b);
	
	cout << a << endl;
	cout << b << endl;
	
	int c = 14;
	cout << c << endl;
	cout << &c << endl;
	c = passAndReturn(c);
	cout << c << endl;
	cout << &c << endl;
	
}
	
//pass by value //pass a copy of variable
void passByValue(int var){
	var = 99;
}

//pass by reference //pass in variable 's address, direct access
void passByRef(int *var){
	*var = 66; //change original value
}

int passAndReturn(int var){
	var = 1;
	return var;
}
Topic archived. No new replies allowed.