Functions have ..........
A.Local scope
B.Block scope
C.File scope
D.Function scope
Answers
➡️Option (C) File Scope is the right answer ✔️
Answer:
Scope refers to the accessibility of a variable. There are four types of scopes in C++ 1. Local Scope 2. Function Scope 3. File Scope 4. Class Scope 1. Local Scope: A local variable is defined within a block. A block of code begins and ends with curly braces {}. The scope of a local variable is the block in which it is defined. A local variable cannot be accessed from outside the block of its declaration. A local variable is created upon entry into its block and destroyed upon exit; Example: int main( ) { int a,b; //Local variable } 2. Function Scope: The scope of variable within a function is extended to the function block and all sub-blocks therein. The lifetime of a function scope variable is the lifetime of the function block. Example: int. sum(intx, int y); //x and y has function scope. 3. File Scope: A variable declared above all blocks and functions (including main()) has the scope of a file. The lifetime of a file scope variable is the lifetime of a program. The file scope variable is also called as global variable. Example: #include using namespace std; int x,y; //x and y are global variable void main() { …….. 4. Class Scope: Data members declared in a class has the class scope. Data members declared in a class can be accessed by all member functions of the class. Example: Class example { int x,y; //x and y can be accessed by print() and void(): void print(); Void total(); };
#SPJ2