In short, a scope is an area in which names can be declared. Names declared in scope are available within that scope and, in some cases, also externally.
(To be pedantically accurate, this is actually the scope of the declaration, and the scope of the name is part of the program in which the name is valid. It starts where it is declared and includes the rest of this region and, sometimes, in some other regions.)
The scope is represented by namespaces, classes, and compound statements (that is, blocks of code statements surrounded by {}
). The latter includes body functions.
Most objects and functions have names, and each of these names is inside a scope.
Thus, the "scope of a function" can mean two things: either the area defined by the body of the function in which its local variables are declared; or the scope (class or namespace) in which the function name is declared.
UPDATE: you say you mean the scope of the function name. It always starts immediately after the announcement; where it ends depends on where this announcement was.
- If it is declared inside a namespace, it lasts until that namespace is closed. If the namespace is reopened in the same translation unit, it returns to scope.
- If it is declared inside the class definition as a member function, then the scope continues until the end of the class definition. It also enters into the scope within the definitions of any derived classes, as well as within the definitions of a member of this class or derived classes.
- If it is declared inside the class definition as a friend or inside the function definition, then the name is actually declared in the surrounding namespace, and the scope coincides with this case.
source share