Computer Science, asked by 5253, 1 year ago

what are scope rules?
class 11th
reply me fast
no silly answers
it's my exam today
if it will correct than I will ark it as brainloest

Answers

Answered by farhansyeed1024
4
Since a main program could contain many functions and in fact a function can contain other functions (i.e., nested functions), one may ask the following questions:

Could a function use a variable declared in the main program?Could a main program use a variable declared in one of its function?The scope rules answer these questions. In fact, scope rules tell us if an entity (i.e., variable, parameter and function) is "visible" or accessible at certain places. Thus, places where an entity can be accessed or visible is referred to the scope of that entity.

The simplest rule is the following:

Scope Rule 1The scope of an entity is the program or function
in which it is declared.

Therefore, in the following, the scope of parameter PI and variables m and nis the main program, the scope of formal argument k and REALvariables f and g is function Funct1(), and the scope of formal arguments uand v is function Funct2().

PROGRAM Scope_1 IMPLICIT NONE REAL, PARAMETER :: PI = 3.1415926 INTEGER :: m, n ................... CONTAINS INTEGER FUNCTION Funct1(k) IMPLICIT NONE INTEGER, INTENT(IN) :: k REAL :: f, g .......... END FUNCTION Funct1 REAL FUNCTION Funct2(u, v) IMPLICIT NONE REAL, INTENT(IN) :: u, v .......... END FUNCTION Funct2 END PROGRAM Scope_1

There is a direct consequence of Scope Rule 1. Since an entity declared in a function has a scope of that function, this entity cannot be seen from outside of the function. In the above example, formal argument k and variables f and g are declared within functionFunct1(), they are only "visible" in function Funct1() and are not visible outside of Funct1(). In other words, since k, f and g are not "visible" from the main program and functionFunct2(), they cannot be used in the main program and function Funct2(). Similarly, the main program and function Funct1() cannot use the formal arguments u and v and any entity declared in function Funct2

Scope Rule 2A global entity is visible to all contained functions,
including the function in which that entity is declared.
Similar questions