A Closer Look

 

Now that we have performed the necessary steps to create a working SPL routine, let's examine the celsius function more closely. We repeat the function here for reference:

 

/* convert Fahrenheit to centigrade */

celsius(f)
{
    local c;
    
    c = (5.0 / 9.0) * (f - 32.0);
 
    return(c);
}

 

The first line of the function

 

/* convert Fahrenheit to centigrade */

 

is a comment, which in this case provides a brief description of what the function does. Characters between the /* and */ are ignored and are used to make the function more understandable. One line comments may also be written as follows:

 

// convert Fahrenheit to centigrade

 

All characters following the // on the same line are ignored. Comments may be used freely and placed anywhere a blank space may ordinarily appear.

 

The name of the function, celsius, is followed by the argument list. The argument list, if any, must be enclosed in parentheses, ( ), and immediately follow the function name. Our celsius function has one argument, the variable f that specifies the input temperature in Fahrenheit. Like C/C++, the body of the function must be enclosed in braces, { }.

 

The variable c contains the result of the temperature conversion. Both variables f and c are local to the celsius function: other routines may use the same names without conflict. In general, SPL is a non-declarative language. Specifying the type of variable (integer, float, array, etc.) is not required.

 

To create a variable in SPL, simply assign it a value using the standard = operator. Even though SPL is non-declarative, we use the optional local statement to explicitly indicate the variable c is local to the celsius function.

 

The result of the conversion is returned by the return statement. Any syntactically correct statement may occur within the parenthesis of the return statement. In this case, the conversion could have been returned directly as:

 

return((5.0 / 9.0) * (f - 32.0));

 

but often returning the value of a variable results in clearer code. A function need not return a value: a return statement with no expression causes control to be returned as does "falling off the end" of a function by reaching the terminating parenthesis.

 

As indicated by the examples, the celsius function can operate on scalar numbers or entire series. If the input is a series, the function automatically converts each point of the series without for or while loops. This extremely powerful feature greatly reduces the complexity of SPL functions compared to traditional languages such as C/C++ or FORTRAN.