Block (programming)


In, a block or code block is a lexical structure of source code which is grouped together. Blocks consist of one or more declarations and statements. A programming language that permits the creation of blocks, including blocks nested within other blocks, is called a block-structured programming language. Blocks are fundamental to structured programming, where control structures are formed from blocks.
The function of blocks in programming is to enable groups of statements to be treated as if they were one statement, and to narrow the lexical scope of objects such as variables, procedures and functions declared in a block so that they do not conflict with those having the same name used elsewhere. In a block-structured programming language, the objects named in outer blocks are visible inside inner blocks, unless they are masked by an object declared with the same name.

History

Ideas of block structure were developed in the 1950s during the development of the first oyone alone, and were formalized in the Algol 58 and Algol 60 reports. Algol 58 introduced the notion of the "compound statement", which was related solely to control flow. The subsequent Revised Report which described the syntax and semantics of Algol 60 introduced the notion of a block and block scope, with a block consisting of " A sequence of declarations followed by a sequence of statements and enclosed between begin and end..." in which "very declaration appears in a block in this way and is valid only for that block."

Syntax

Blocks use different syntax in different languages. Two broad families are:
Some languages which support blocks with declarations do not fully support all declarations; for instance many C-derived languages do not permit a function definition within a block. And unlike its ancestor Algol, Pascal does not support the use of blocks with their own declarations inside the begin and end of an existing block, only compound statements enabling sequences of statements to be grouped together in if, while, repeat and other control statements.

Basic semantics

The semantic meaning of a block is twofold. Firstly, it provides the programmer with a way for creating arbitrarily large and complex structures that can be treated as units. Secondly, it enables the programmer to limit the scope of variables and sometimes other objects that have been declared.
In primitive languages such as early Fortran and BASIC, there were a few built-in statement types, and little or no means of extending them in a structured manner. For instance, until 1978 standard Fortran had no "block if" statement, so to write a standard-complying code to implement simple decisions the programmer had to resort to gotos:

C LANGUAGE: ANSI STANDARD FORTRAN 66
C INITIALIZE VALUES TO BE CALCULATED
PAYSTX =.FALSE.
PAYSST =.FALSE.
TAX = 0.0
SUPTAX = 0.0
C SKIP TAX DEDUCTION IF EMPLOYEE EARNS LESS THAN TAX THRESHOLD
IF GOTO 100
PAYSTX =.TRUE.
TAX = * BASCRT
C SKIP SUPERTAX DEDUCTION IF EMPLOYEE EARNS LESS THAN SUPERTAX THRESHOLD
IF GOTO 100
PAYSST =.TRUE.
SUPTAX = * SUPRAT
100 TAXED = WAGES - TAX - SUPTAX

Even in this very brief Fortran fragment, written to the Fortran 66 standard, it is not easy to see the structure of the program, because that structure is not reflected in the language. Without careful study it is not easy to see the circumstances in which a given statement is executed.
Blocks allow the programmer to treat a group of statements as a unit, and the default values which had to appear in initialization in this style of programming can, with a block structure, be placed closer to the decision:


if wages > tax_threshold then
begin
paystax := true;
tax := * tax_rate

if wages > supertax_threshold then
begin
pays_supertax := true;
supertax := * supertax_rate
end
else begin
pays_supertax := false;
supertax := 0
end
end
else begin
paystax := false; pays_supertax := false;
tax := 0; supertax := 0
end;
taxed := wages - tax - supertax;

Use of blocks in the above fragment of Pascal clarifies the programmer's intent, and enables combining the resulting blocks into a nested hierarchy of conditional statements. The structure of the code reflects the programmer's thinking more closely, making it easier to understand and modify.
The above source code can be made even clearer by taking the inner if statement out of the outer one altogether, placing the two blocks one after the other to be executed consecutively. Semantically there is little difference in this case, and the use of block structure, supported by indenting for readability, makes it easy for the programmer to refactor the code.
In primitive languages, variables had broad scope. For instance, an integer variable called IEMPNO might be used in one part of a Fortran subroutine to denote an employee social security number, but during maintenance work on the same subroutine, a programmer might accidentally use the same variable, IEMPNO, for a different purpose, and this could result in a bug that was difficult to trace. Block structure makes it easier for programmers to control scope to a minute level.

;; Language: R5RS Standard Scheme

)


;; Within this lambda expression the variable empno refers to the ssn
;; of an underling. The variable empno in the outer expression,
;; referring to the manager's ssn, is shadowed.

))
))))

In the above Scheme fragment, empno is used to identify both the manager and his or her underlings each by their respective ssn, but because the underling ssn is declared within an inner block it does not interact with the variable of the same name that contains the manager's ssn. In practice, considerations of clarity would probably lead the programmer to choose distinct variable names, but he or she has the choice and it is more difficult to introduce a bug inadvertently.

Hoisting

In a few circumstances, code in a block is evaluated as if the code were actually at the top of the block or outside the block. This is often colloquially known as hoisting, and includes: