Scorpio News |
October–December 1988 – Volume 2. Issue 4. |
Page 12 of 35 |
---|
char scrn_val ( x,y )
int x, y;
{
/* Code for the function in here */
}
Local variables are defined within the function body along with the code, which consists of a list of statements. There are basically four types of statement, which are:–
an expression to evaluate
a pre-defined construct (e.g. while)
a compound statement
the null statement
An interesting note here is that there is no assignment statement, and technically speaking an expression is only evaluated (and thrown away) and not assigned. However, assignment can be carried out as an operator forming part of the evaluation. This allows some very interesting code such as:
sum = a-- + ( b = x / y ) + ( c = 2 * d );
(This results in b being set to x/y, c being set to 2*d, sum being set to a+b+c (after the previous assignments) and a being decremented.) Note that expressions must be followed by a semi-colon to form a statement. A semi-colon alone is the null statement. Compound statements can be made by enclosing any list of statements within braces (“{” and “}”).
There are five defined constructs available :–
if ( expression ) statement else statement
while ( expression ) statement
do statement while ( expression );
for ( expression_OPT; expression_OPT; expression_OPT ) statement
switch ( expression ) statement
The else section of an if construct is optional. All three expressions in a for statement are optional, but the semicolons are not and neither are any of the parentheses shown above.
Within the “statement” part of some of the above there are some other items allowed. These are :–
Page 12 of 35 |
---|