Mutiple files return value issue

So I am toying with multiple files and learning how to link them together and I have run into an issue I cant locate a solution. So this is as short as possible I will show the .h example, I linked a .cpp as well, same results. Real basic add program that get input from user.

**note: i am using 5 as all my inputs because thats easy to add, and all my code comments reflect this.

multi1.cpp

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
#include <iostream>
#include "multi2.h"

	
	int x = 0;

int function1();

int main()
{	
	int a = 0;
	int b = 0;
	using namespace std;                                                          // I make comments
		cout << "hello world!" << endl;
		cout << "Enter 2 Single Digit Numbers"  << endl;
			cin >> a;
			cin >> b;
			x = a + b;
			cout << x << endl;               //checking math and its ok
			function1(x);
			cout << x << endl;                                              //how is this 10 again??
			cout << "Your total all added together is " << x << endl;
		cout << "Press any enter to continue....."  << endl;
		cin.clear();
		cin.ignore(255, '\n');
		cin.get();
	return 0;
	
}


and the multi2.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef MULTI2_H       //header gaurds
#define MULTI2_H

int a = 0;
int b = 0;


int function1(int x)
{
	using namespace std;
		cout << "Are you having fun yet?"  << endl;
		cout << "Gimme 2 more please"  << endl;
			cin >> a;
			cin >> b;
			x = x + a + b;
			cout << x << endl;          //math check here and still good
	return x;		
}

#endif 


and the output:


hello world!
Enter 2 Single Digit Numbers
5 <- cin a
5 <- cin b
10 <- first output
Are you having fun yet?
Gimme 2 more please
5 <- cin a
5 <- cin b
20 <- so its passing 10 to function1 in the .h
10 <- but Main still thinks this is 10
Your total all together is 10 <- wrong!!



So I know this is probaly some alternate type of return statement in then .h file that I don't know about yet, or this is some other type of fundemental issue. Please steer me in the right direction. Thank You!
You don't do anything with the return value. If you want x in main to get the value that is returned from the function you have to assign it.
x = function1(x);
Almost worked, exept when run the program calls the function a second time. How do I assign function1 value for x without another recall? Thank you, getting me closer.

**DOH!! im an idiot, wrong place, haha. Thanks!
Last edited on
Topic archived. No new replies allowed.