Parameter-passing

Hi everyone, I'm new here so I'm I'm doing something wrong please let me know, but I really just need help with a practice exam problem. If someone could walk me through what this code is doing (or at the very least what the (int&) portions are doing, that would greatly help me, because I'm not sure and have looked everywhere for an answer. The practice question is to "Name and explain the parameter-passing disciplines...and give the output of the code."

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
  #include <iostream>
using std::cout; using std::cin; using std::endl; 

int function1(int);
int function2(int&);


int main() {
	int a = 3, b = 4;
	int c = function1(a);
	int d = function2(b);
	cout << a << b << c << d;
	
} 

int function1(int aa) {
	aa++; 
	return aa;
}

int function2(int &bb) {
	bb++;
	return bb;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using std::cout; using std::cin; using std::endl; 

int function1(int); // This function takes a copy of the data-type that is passed through it and modifies it.
int function2(int&); // This function takes the original data-type and modifies it.


int main() {
	int a = 3, b = 4; // Declare and init 2 variables of type int
	int c = function1(a); // the variable c is now equal to whatever value function1 returns, but it DOES NOT modify the value of variable a since it was passed as a copy.
	int d = function2(b); // the variable d is now equal to whatever value function2 returns. Since function2 is passed the actual variable itself (denoted by the ampersand), variable b is modified.
	cout << a << b << c << d;
	
} 

int function1(int aa) {
	aa++; 
	return aa;
}

int function2(int &bb) {
	bb++;
	return bb;
}
Topic archived. No new replies allowed.