Passing by reference FUNCTION

So I completed a program identical to this one except it passes pointers ( Which I run and compile perfectly). I am having some trouble with passing (2) references. I can pass 1 reference but right now I can't pass 2.

Here is my code so far and I am getting too few functions in call when I declare int result = incr10(num);


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
44
45
46
47
48
49
#include <iostream>

using std::cout;
using std::endl;


int incr10(int& num, int& num2); 

int main(void)
{
int num = 3;
int num2 = 6;

int result = incr10(num);

cout << endl;
cout << "Incr10(num) :: " << result;
cout << endl;
cout << "First number after increment :: " << num;
cout << endl;


int result2 = incr10(num2);

cout << endl;
cout << "Incr10(num2) :: " << result2;
cout << endl;
cout << "Second number after the increment :: " << num2;
cout << endl;


return 0;


}

incr10(int& num, int& num2) 
{
cout << endl;
cout << "Starting number 1 :: " << num;
cout << endl;
cout <<" Starting number 2 :: "<< num2;
cout << endl;

num += 10; 
num2 += 10;

return num; 
}

Last edited on
int result = incr10(num);

If you're going to pass 2 different variables, you need to give it 2 different variables. Right now you're only passing 1.

Do this instead:

int result = incr10(num,num2);
::::FACEPALM ugh. Lol so simple thanks.

The only problem now is the second number after increment is reading 26???? Any ideas as to why? I can't see why it would be reading that unless it just doubled 13 which is number 1 after increment? Thanks much

EDIT: It is also showing starting number 1:: two times and also the same with starting number 2.
Last edited on
I think it's because you are calling your function twice, which would make num2 = 26;
I fixed my code a little bit could you see what the problem would be by chance? thanks much.
How am I calling my function twice?

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
44
45
46
47
48
49
#include <iostream>

using std::cout;
using std::endl;


int incr10(int& num, int& num2); 

int main(void)
{
int num = 3;
int num2 = 6;



int result = incr10(num,num2);


cout << endl;
cout << "First number after increment :: " << result;
cout << endl;


int result2 = incr10(num2,num);


cout << endl;
cout << "Second number after the increment :: " << result2;
cout << endl;


return 0;


}

int incr10(int& num, int& num2) 
{
cout << endl;
cout << "Starting number 1 :: " << num;
cout << endl;
cout <<" Starting number 2 :: "<< num2;
cout << endl;

num += 10; 
num2 += 10;

return num;
}

Last edited on
I have added comments in your code to give you a clue as to where the problem is:
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
44
45
46
47
48
49
#include <iostream>

using std::cout;
using std::endl;


int incr10(int& num, int& num2); 

int main(void)
{
int num = 3;
int num2 = 6;



int result = incr10(num,num2);  // Function call #1


cout << endl;
cout << "First number after increment :: " << result;
cout << endl;


int result2 = incr10(num2,num);  // Function call #2


cout << endl;
cout << "Second number after the increment :: " << result2;
cout << endl;


return 0;


}

int incr10(int& num, int& num2) 
{
cout << endl;
cout << "Starting number 1 :: " << num;
cout << endl;
cout <<" Starting number 2 :: "<< num2;
cout << endl;

num += 10; 
num2 += 10;

return num;
}
haha thanks much. I didnt even realize that .... >.>. I figured that one would call num and one would call num2. Thanks again and I have learned my lesson with functions lol.
Topic archived. No new replies allowed.