It should not be global I am sure of that, though you must understand that you must learn about scopes and how to use functions. You aren't using functions right if everything you are changing with the function is a global variable. You should be passing variable by reference and by value into functions and returning or not returning your results.
Check out the function tutorial on this site to learn more. But here is a quick example. (I wouldn't really use functions like this but this is a example)
Lets say I had a struct called "Player" it held all my stats for a player in my game.
1 2 3 4 5
|
struct Player {
std::string name;
int attack;
int health;
};
|
Now lets say I want to have a function that sets up the stats for the player (Like health, name and attack) that I have just created. I might do something like this.
1 2 3 4 5
|
void setUpPlayer(Player &newPlayer) {
newPlayer.name = "Brandon";
newPlayer.attack = 15;
newPlayer.health = 100;
}
|
I would then call this function like
setUpPlayer(brandon); // Where brandon is the objects name we created
.
What this function does is sets up the stats for the new player object we created by using a reference which mean we directly alter whatever object we pass into the function as a argument.
The whole thing would look maybe something like 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
|
#include <string>
#include <iostream>
struct Player {
std::string name;
int attack;
int health;
};
void setUpPlayer(Player &newPlayer) {
newPlayer.name = "Brandon";
newPlayer.attack = 15;
newPlayer.health = 100;
}
int main() {
// Creates my new object
Player brandon;
// Calls my function to setup the stats of my player
setUpPlayer(brandon);
// Just shows what all the members of the object are set to
std::cout << "Name: " << brandon.name << std::endl;
std::cout << "Attack: " << brandon.attack << std::endl;
std::cout << "Health: " << brandon.health << std::endl;
}
|
Now this is not the best way to create stuff like this and you should instead use classes to handle these things but I just wanted to show how you can use functions without having every objects or variable that the function alters have to be in the global namespace. So go checkout the functions tutorial it is actually very helpful and functions are very powerful.
Also sorry if there are mistakes in the code ;p have been coding in Python for the last few months almost exclusively. And if you need any help with anything chippz just let me know I would be glad to help answer any questions you have.