Declaring variables, classes, etc.

When writing code and one has to declare a variable or class, is it good programming practice to declare it outside the main function or inside the main function? So far I have been declaring class names and its associate object outside the main function as well as variables. If I am in a specific situation where the scope of the variable matters, I will declare it elsewhere, but for general knowledge and knowing what to do and what not to do, I am unsure about it.
Normally you should declare variables in as local a scope as possible, which means that you should usually declare the variable a function close to first use. It is considered bad practice to use global variables, except in rare circumstances.

You will usually define a function prototype or a class outside any function so that the function or class can be used anywhere in the program.

So as an example is there anything wrong with how the declarations are made here? Aside from creating the class in the main.cpp file.
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
#include <iostream>
#include <string>
#include <fstream>

class Person
{
private:

public:
};

int add(int a, int b);
int sum;

int main()
{
	Person person;

	sum = add(1, 2);

	return 0;
}

int add(int a, int b)
{
	int sum = a + b;
	return sum;
}
Last edited on
closed account (E0p9LyTq)
While it is not wrong, global variables (sum declared outside of main or add) are not something that should normally be done if at all possible.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Person
{
private:

public:
};

int add(int a, int b);

int main()
{
   Person person;

   int sum = add(1, 2);
}

int add(int a, int b)
{
   int sum = a + b;
   return sum;
}

If you aren't going to use a header it is best to not include it. It won't cause problems, just a waste of effort.
Sometimes it's just more convenient to use global variables. Like if for example you have a bunch of variables that persist throughout the program and have to be passed to all sorts of functions then I would rather use global variables (wrap them in a namespace of you want). It makes the function calls much more readable and at the same time it isn't at the cost of anything.
It makes the function calls much more readable and at the same time it isn't at the cost of anything.

Why do you think using global variables makes function calls more readable? IMO, it is the opposite, except in very trivial programs.

By the way there is a definite cost of using global variables, trouble shooting a program littered with multiple global variables is much more difficult, and it is easier for a bug to find it's way into your program because the value of those global variables can change anywhere.



If you have a lot of constants that are determined run-time and that need to be passed around to different functions then I have the preference of keeping them global. It wouldn't be hard to debug as they are constants. And it would make the program more neat to have them global because you wouldn't have to pass around those constants from main function to other functions.

But this my opinion. I just wanted to say that you should not live and die by the rule that you shouldn't have global variables. There can be situations where they are more appropriate and there will be situations where they're just more convenient.
If you have a lot of constants that are determined run-time and that need to be passed around to different functions then I have the preference of keeping them global. It wouldn't be hard to debug as they are constants.

First you didn't mention the constant aspect in your last post, having compile time constants global is not a big problem.

But what do you mean by "determined at run-time"?

There can be situations where they are more appropriate and there will be situations where they're just more convenient.

Yes there are reasons to use global variables but those reasons are few and far between, "convenience" is not, IMO, a valid reason in and of itself.

My rule is that constants can go anywhere (should be in a namespace, if the program is large) including the global scope, but never a variable.

there is only 1 really good use case for a global variable. They can provide a performance boost in low level code under a few very specific scenarios. Just about everything else is an indication of a design problem. And if you do find that using one has some sort of merits, wrap it up tight; put it in a class by itself as a static member, for example.
Thanks for the replies, I think I will leave it to discretion depending on the situation to determine whether I should use it or not. But I will also remember why it is not a good practice.
Topic archived. No new replies allowed.