variable scope

I'm aware of the "avoid global variables" rule (and how you can still use them in specific cases and when you know exactly what you're doing), but I also know that a variable can only be used if it was declared before the code block trying to use it.

So I know in this case a is a global scoped variable that is visible in both functions:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
int a=10;
void function1()
{
    a=5;
}
int main()
{
    function1();
    std::cout << a;
}


But this is where I start to get confused:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void function1()
{
    a=5;
}

int a=10;
int main()
{
    function1();
    std::cout << a;
}


In this case I'll get an error saying that variable a wasn't declared in function1's scope.

So my first question is: on the second example, is variable a still a global variable?

Now for a third example, since the variable isn't on function1's scope, let's pass it as an argument:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void function1(int & x)
{
    x=5;
}

int a=10;
int main()
{
    function1(a);
    std::cout << a;
}


Is it good or bad practice (or maybe neither) to declare variable a that way? Are there any pros or cons of declaring variables like this?
I've seen code where variables that should be local to main are declared right before the main function definition and I don't see why they chose to do it like that.

Thanks.
Last edited on
So my first question is: on the second example, is variable a still a global variable?
Yes.

Is it good or bad practice (or maybe neither) to declare variable a that way?
As a general rule, global variables should be avoided. In this particular example, you're not using the "globalness" in any particular way, so making it global is pointless. Since one should strive to have as little pointlessness in a program as possible, arguably yes, this particular declaration would be bad practice.

I've seen code where variables that should be local to main are declared right before the main function definition and I don't see why they chose to do it like that.
Probably because those people were bad programmers.
on the second example, is variable a still a global variable?

It is a global, but one defined AFTER your function.

Same idea as why:
1
2
3
4
5
6
7
8
9
void some_func()
{
   // do something
}

int main()
{
   some_func();
}

or
1
2
3
4
5
6
7
8
9
10
11
void some_func();

int main()
{
   some_func();
}

void some_func()
{
   // do something
}

works. You are declaring or forward-declaring a function before it is used in main.

What should be obvious is why this (and your second example) won't work:
1
2
3
4
5
6
7
8
9
int main()
{
   some_func();
}

void some_func()
{
   // do something
}


The function, or the global variable, isn't defined before being used.

Passing a reference (or pointer) into a function changes the "read from top to bottom" nature of how the compiler creates the machine code from your source code.

This is why header files need to be included at the top of your source.
Last edited on
So my first question is: on the second example, is variable a still a global variable?
Since the program won't compile, I'd argue that the question is meaningless. For example, in the code below, is var global variable?
1
2
3
4
5
6
7
8
{
int var;
}

int main()
{
   cout << var;
}


You could argue that it's inside a block so it's local. But since the program won't compile, the whole question is meaningless.

Regarding global variables:
- If anyone ever tells you that they should always be avoided, ask them why cin, cout and cerr are global variables.
- Usually when I use global data it stores configuration info or program context or environment - The sort of thing that you need to consult all over the code.
Topic archived. No new replies allowed.