Tech

Scope Rules–Functions–STRUCTURED PROGRAMMING Course Notes

Scope Rules

  • Unlike automatic variables, static local variables retain their values when the function in which they’re declared returns to its caller.
  • An identifier declared outside any function or class has global namespace scope.
  • Labels are the only identifiers with function scope. Labels can be used anywhere in the function in which they appear, but cannot be referenced outside the function body. (Labels are followed by a colon. ‘:’ )
  • Identifiers declared inside a block have local scope. Local scope begins at the identifiers declaration & ends at the terminating right brace (‘}’) of the block in which the identifier is declared.
  • Identifiers in the parameter list of a function prototype have function-prototype scope.

Note: Avoid variable names that hide names in outer scopes. This can be accomplished by avoiding the use of duplicate identifiers in a program.