Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does scope of functions mean?

Tags:

c++

scope

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?

like image 209
Swathi Appari Avatar asked Jul 05 '12 13:07

Swathi Appari


People also ask

What are the 3 types of scope?

There are three types of scope in JavaScript — 1) Global Scope, 2) Function Scope, and, 3) Block Scope.

What is function scope in programming?

An important idea in programming is scope. Scope defines where variables can be accessed or referenced. While some variables can be accessed from anywhere within a program, other variables may on… Blocks and Scope.

What is difference between scope and function?

The scope determines the accessibility of variables and other resources in the code, like functions and objects. JavaScript function scopes can have two different types, the locale and the global scope. Local variables are declared within a function and can only be accessed within the function.

What is the scope of a function in C++?

Function scope A label has function scope, which means it is visible throughout a function body even before its point of declaration. Function scope makes it possible to write statements like goto cleanup before the cleanup label is declared.


1 Answers

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.
like image 84
Mike Seymour Avatar answered Oct 06 '22 01:10

Mike Seymour