An Introduction to Defining Code Blocks in Various Procedural Programming Languages
by Guy Lecky-Thompson
A code block is just a piece of code that is contained in some way, and typically should be kept as one unit of execution.
For example, the result of a binary decision typically manifests itself in one of two outcomes; code that is executed if the decision evaluated to 'TRUE' and another if the result was 'FALSE'. Now, you wouldn't want to confuse bits of these code blocks, or get them swapped, or execute half of one, and half of the other, so we need some way to keep the lines together.
Different languages handle this in different ways.
For example, C styled languages enclose their code blocks in brackets, otherwise known as curly braces:
if (1==1) {
// do something
}
Languages that have their roots in BASIC tend to use the END keyword, as in:
IF 1 = 1 THEN REM Do something here ENDIF
Of course, where C styled languages use the same approach (curly braces) for all code blocks, most other languages vary their structure according to whether the code block is conditionally executed, is part of a loop, or something else.
Here are a few examples (the ellipsis is used to denote multiple lines of code):
- WHILE I% < 10... WEND (VBA, VB Script)
- REPEAT... UNTIL (I% >= 10) (BBC Basic version of the above)
- FOR I%:= 1 TO 10... NEXT I% (BBC Basic and variants)
- BEGIN... END (Modula-2, Pascal)
The last is used in the same way as curly braces in C styled languages; that is, there are multiple code constructs using BEGIN... END to denote the code block, While loops and Procedures.
Procedures, Subroutines and Functions: Named Code Blocks
There's a special kind of code block that I call a named code block.
It is one which can be called by name. In C styled languages these are called functions, and pretty much every other procedural language makes a distinction between Functions (which return a value) and Procedures (which do not).
Subroutines are a throwback to the days of line-number programming, where you might say GOSUB 1000 to send execution off to a code block which ended with the keyword RETURN (BBC Basic), and we're ignoring modern constructs like Modules and Object Oriented concepts such as methods in this discussion.
Nevertheless, we need some way to tell the compiler or interpreter where the code starts and ends, and in C styled languages, we just enclose it in curly braces:
void SomeFunction ()
{
// code to do something useful
}
Line by line, the Modula-2 version of this would be:
PROCEDURE SomeProc; BEGIN(* code to do something *) END SomeProc.
Just for completeness, here it is in Pascal:
Procedure SomeProc; Begin{do something} End.
The reason the above is particularly interesting is that it shows the use of curly braces in Pascal as comment block delimiters. However, the astute student will note that the ISO 7185:1990 standard also allows (* and *) in the same way that Modula-2 two comments are constructed.
To round off this discussion, note that function definitions in Modula-2 / Basic / Pascal styled languages work in roughly the same way. The pseudocode would look something like:
Function ReturnsAValue As Value[do something]Return Value; End Function.
The exact syntax will vary, but a simple web search will yield the variations by language as required.
No comments:
Post a Comment