7

What does scope of functions mean?

I understand the scope of variables. When we talk about the scope of functions, is it referred to the functions within structures (classes) or is there scope for the normal functions that we call in main() of a C/C++ program?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Swathi Appari
  • 193
  • 1
  • 1
  • 12
  • 3
    Where did you encounter that wording ? – cnicutar Jul 05 '12 at 13:47
  • 2
    Note that there's the _scope of the function itself_ (where is this identifier valid? — which is the same as the scope of a variable) and _function scope_, which is the scope for another identifier spanned by a function (a variable defined within a function is valid between `{` and `}`). – sbi Jul 05 '12 at 13:55
  • 1
    @sbi, good point, the good answers here address only the first definition, but the question could be about the other one. – unkulunkulu Jul 05 '12 at 13:56

4 Answers4

9

Loosely speaking, a scope is a region in which names can be declared. Names declared in a scope are accessible within that scope and, in some circumstances, also from outside.

(To be pedantically accurate, that is actually a declaration region, and the scope of a name is the part of the program in which the name is valid. It begins where it's declared, and includes the rest of that region and, sometimes, some other regions.)

Scopes are introduced by namespaces, classes, and compound statements (that is, blocks of code statements surrounded by {}). The last one includes the bodies of functions.

Most objects and functions have names, and each of these names is inside a scope.

So the "scope of a function" could mean two things: either the scope defined by the function's body, in which its local variables are declared; or the scope (either a class or a namespace) in which the function name is declared.

UPDATE: you say you mean the scope of the function name. This always begins immediately after the declaration; where it ends depends on where that declaration was.

  • If it is declared inside a namespace, it lasts until that namespace is closed. If the namespace is reopened later in the same translation unit, then it comes back into scope there.
  • If it is declared inside a class definition as a member function, then the scope lasts until the end of the class definition. It is also in scope inside the definitions of any derived classes, and also inside the definitions of member of that class or derived classes.
  • If it is declared inside a class definition as a friend, or inside a function definition, then the name is actually declared in the surrounding namespace, and the scope is the same as that case.
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Doesn't a scope actually mean a region where names can be _accessed_, i.e. a scope can span multiple blocks for example. At least scope of a variable starts at the point where it's declared. – unkulunkulu Jul 05 '12 at 13:53
  • @unkulunkulu: Technically, its the region in which the name is valid, and I'm using it slightly loosely to refer to a *declaration region*. I'll see if I can make the answer a bit more correct without getting too complicated. – Mike Seymour Jul 05 '12 at 13:56
  • yeah, bad wording by me, my main point is that the 'declared' word is not 100% correct either. – unkulunkulu Jul 05 '12 at 13:57
  • Thanks, i meant the scope in which function name declared as u mentioned – Swathi Appari Jul 06 '12 at 05:54
  • @SwathiAppari: OK, I've given more detail about that. – Mike Seymour Jul 06 '12 at 10:43
  • "sometimes, some other regions." How?? – Don Larynx May 23 '15 at 15:51
  • @DonLarynx: namespaces can be closed and reopened, so that their scope can cover several regions. Class scope covers the class definition and the definitions of member functions and nested classes. – Mike Seymour May 24 '15 at 00:16
  • Please define region? How is it different from scope? – Don Larynx May 24 '15 at 06:44
  • 1
    @DonLarynx A declaration region is a single contiguous region in which declarations can appear. A scope is a set of perhaps non-contiguous regions in which declared names are accessible. See the standard if you want the precise definitions. – Mike Seymour May 24 '15 at 09:11
7

Functions can have global, namespace, class (usually called members in that case), or local (within another function) scope. They can also be static giving them internal linkage or within an anonymous namespace, making them inaccessible outside a translation unit (while still having external linkage so they can be used as template parameters).

Mark B
  • 95,107
  • 10
  • 109
  • 188
3

Yes, functions have scope as well, though their scope is generally larger than that of most variables.

In C [edit: which was one of the tags when I wrote this], functions have either global scope or file scope. Global scope applies to a normal function that's visible throughout the entire program. File scope applies to a function you've marked as "static", so it's only visible within the same translation unit.

C++ uses slightly different names for those, but has the same basic concepts. It adds namespaces in the form of the things actually named "namespace" and structs/classes. With one exception, a function in a namespace is visible only within that namespace. The exception is if you define a friend function inside a class/struct:

class X {
    friend void whatever(X const &) { do_something(); }
};

In this case, even though the function is defined inside of X, its name is injected into the surrounding namespace so it's visible outside of X.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

I think what is meant here is the scope in which Labels (A Label is what you use with goto statements) are visible.

Maybe this article could also help you understand scopes. You can also take a look at this question on stackoverflow.

Community
  • 1
  • 1
SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105