global variables vs local variables

Good morning all.

I'm a newbie in c++ and i'm writing a prog to exchange from a currency to another.

In my prog i use a variable and an array in almost all my functions, so my question is if it would be better to declare them both as global, so that they were visible to all and i didnĀ“t need to pass them as arguments to functions, or, instead, as local to main and pass them as arguments in function calls.

thanks in advance for the help.

disgrace
would be better to declare them both as global
No. It's almost always never better to use globals.

It may seem reasonable in the context your write the code, but it's a maintenance nightmare, and parts of your code is coupled the globals, making it difficult to reuse that code in other programs.
Thanks kbw for the quick answer.

I didn't use globals so far.
But as you said "it seemed reasonable".
Nevertheless i wasn't confortable with that, hence my question.

I'll avoid them then.
closed account (j3Rz8vqX)
Alternative:
Namespaces can be used as pseudo-global; the conflicting variable counts may significantly decrease.

Example:
1
2
3
4
5
6
7
8
namespace myGlobals_x001
{
    struct data{
        string str;
        int data;
    };
    vector<data> datas;
}
Last edited on
Main problem with globals is global state, not name conflicts.
Second problem is static initialization order fiasco.
Tahnks all for the replies!
Topic archived. No new replies allowed.