Block (programming): Difference between revisions

Content deleted Content added
Remove two instances of "he or she"
Slight reword so we don't have to say "variables and other objects", just say "objects"
Line 5:
In [[computer programming]], a '''block''' or '''code block''' is a lexical structure of [[source code]] which is grouped together. Blocks consist of one or more [[Declaration (computer programming)|declarations]] and [[Statement (computer science)|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 structure]]s 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 variablesthose having the same name used elsewhere in a program for different purposes. In a block-structured programming language, the names of variables and other objects such as procedures which are declarednamed in outer blocks are visible inside other inner blocks, unless they are [[VariableName shadowingmasking|shadowedmasked]] by an object ofdeclared with the same name.
 
==History==
Line 61:
 
==Limitations==
Some languages which support blocks with variable declarations do not fully support all declarations; for instance many C-derived languages do not permit a function definition within a block ([[nested function]]s). 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==
Line 146:
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:
* [[Loop-invariant code motion]], a compiler optimization where code in the loop that is invariant is evaluated before the loop;
* [[Variable hoisting]], a scope rule in JavaScript, where variables have function scope, and behave as if they were declared (but not defined) at the top of a function.
 
==See also==