Variable declaration location gen ?'s

Hello all,

I am just getting into learing C++, and have a question about variable declaration location in code, and the importance of why they need to be declared in a certain spot. This is just play code for learning purposes.

Here is what works:

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
#include <iostream>

	int x = 0;
	int a = 0;
	int b = 0;

int function2()
{
	using namespace std;
		cout << "I like to add!" << endl;
		cout << "And want 2 more single digit numbers" << endl;
			cin >> a;
			cin >> b;
			x = x + a + b;
			cout << x << endl;
	return 0;			
}

int function1()
{
	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;
				function2();
	return 0;		
}

int main()
{
	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;
				function1();
		cout << "Press any enter to continue....."  << endl;
		cin.clear();
		cin.ignore(255, '\n');
		cin.get();
	return 0;
	
}


What does not work is this and I am having trouble grasping why and finding the answer to this:

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
50
51
52
53
#include <iostream>

int function2()
{
int f2x = f1x;
int f2a = 0;
int f2b = 0;
	using namespace std;
		cout << "I like to add!" << endl;
		cout << "And want 2 more single digit numbers" << endl;
			cin >> f2a;
			cin >> f2b;
			f2x = f2x + f2a + f2b;
			cout << f2x << endl;
	return 0;			
}

int function1()
{
int f1x = x;
int f1a = 0;
int f1b = 0;
	using namespace std;
		cout << "Are you having fun yet?"  << endl;
		cout << "Gimme 2 more please"  << endl;
			cin >> f1a;
			cin >> f1b;
			f1x = f1x + f1a + f1b;
			cout << f1x << endl;
				function2();
	return 0;		
}

int main()
{
int x = 0;
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;
				function1();
		cout << "Press any enter to continue....."  << endl;
		cin.clear();
		cin.ignore(255, '\n');
		cin.get();
	return 0;
	
}


With this code I get the undeclared variables error during compiling. My question is why?

I realize I do not know all there is to know about C++, but this is a concept I cant seem to locate an answer for. For some reason I see the need, somewhere down the road, to carry values for variables into other functions. Since the program starts in MAIN, why does the code break during compiling giving the undeclared variable error?

Am i just being picky? it makes more sence to declare the variable in the first function to make use of it and then have the ability to carry that value over into called functions.

Any explinations are hopefully helpful.
The program may start in main but the compiler doesn't. The compiler starts at the top.

Anyway, inside any function, the following variables exist:
1) Variables created inside that function
2) Global variables
3) Variables passed into that function

So look at this:
1
2
3
int function2()
{
  int f2x = f1x;

f1x does not exist inside this function. The fact that some other function somewhere creates a variable with this name is completely irrelevant.
Last edited on
I was sort of thinking that was it. Thank you, very appriciated. I have more to learn about #3. :)
Topic archived. No new replies allowed.