Reputation: 5
I'm making a program for the game 15 puzzle. My function headers look like this:
void leftSlide(vector< vector<int> >& puzzle);
void rightSlide(vector< vector<int> >& puzzle);
void upSlide(vector< vector<int> >& puzzle);
void downSlide(vector< vector<int> >& puzzle);
my main function also has a vector< vector<int> > puzzle
. Am I allowed to do this, or will this cause problems?
Upvotes: 0
Views: 1515
Reputation: 100
The scope of a variable is within the enclosing curly braces. For example,
void foo()
{
int x; // variable x is not known outside of foo
}
This scoping rule applies even for variables in the argument list. For example,
void boo (int y)
{
// variable y in not known outside of boo
}
Therefore, in your case, the variables will be passed from the main driver to the individual functions by reference. So, yes, you can have variables of the same name in different scopes.
Upvotes: 1
Reputation: 5783
simply yes
The potential scope of a variable introduced by a declaration in a block (compound statement) begins at the point of declaration and ends at the end of the block. Actual scope is the same as potential scope unless there is a nested block with a declaration that introduces identical name (in which case, the entire potential scope of the nested declaration is excluded from the scope of the outer declaration)
Upvotes: 0