<< Click to Display Table of Contents >> Navigation: Explain™ Control Language > Explain™ fundamentals > Functions and Control flow |
Functions in Explain™ are very similar to functions in C or Pascal. As in most modern functional languages, each function has a name, an argument list, and a list of statements to be executed in sequence. Explain, like common procedural languages, uses functions to manage execution flow.
function SwapPrint(A, B)
if (A lt B)
; Exchange A & B
Temp = A
A = B
B = Temp
Count = Show(A, B)
return
function Show(V1, V2)
Printl("1st Value = ",V1)
Printl("2nd Value = ",V2)
V1+V2 ; last evaluated value is returned
return
Note the following Explain features:
•There can be function arguments (e.g., A, B, V1, V2).
•There can be local variables within a function (e.g., Temp).
•There are no BEGIN or END statements. Explain uses indentation (in the form of <TABS>) to indicate blocks of statements.
•There is no end-of-statement marker.
•Explain is case-sensitive. Keywords are in lowercase.
•Comments begin with the semicolon ";" character and end at the end of the line.
Program statements are generally one line long, but they can be continued over several lines. When statements are continued, they must have the same indentation level as the first line of the statement. For example, Function1 below has three parameters:
Function1("Parameter1 is very long title string which may not be shown completely if you do not scroll to the right and look at it.",param2, param3)
This can make the code difficult to read. You can force the compiler to disregard indentation changes by putting an ampersand "&" as the first character on a line, for example:
Function1("Parameter1 is still a very long title string but will be shown
& completely because we used a continue symbol.", param2, param3)
As you can see, the second code-segment is easier to understand.
Experienced programmers may be wondering about the variables V1 and Temp, used in the example above. What data type are they? Are they bytes, integers, long integers, reals or double-length reals?
In Pascal, you declare the function as:
function Show(V1, V2 : integer) : integer
whereas in Explain™ it is:
function Show(V1, V2)
In Explain, the type accompanies the piece of incoming data! In this example, the variable V1 in Show acquires the type and value of A in SwapPrint. Also, when the statement Temp = A is executed, the variable Temp acquires the type and value of A. This lack of data-typing for function arguments comes from Explain's Smalltalk heritage.