Program structure
The header, the declarations and the main block of an AlgoML file.
The frame
Algorithm Nameversion '1.0'language Cdeclarationsbegininstructionsendend algorithm
The parts always come in this order:
| Part | Role |
|---|---|
Algorithm Name | The name of the program. Use letters, digits and _, starting with a letter. |
version '1.0' | A version number of your choice, as text. |
language C | The language to generate: C, JAVA or PYTHON. |
| Declarations | Constants, variables, structures, functions and procedures. |
begin ... end | The main block: the instructions run when the program starts. |
end algorithm | The end of the file. |
Declarations
Everything you use must be declared before begin: constants, variables, structures, functions and procedures.
Algorithm Gcdversion '1.0'language Cfunction gcd(a : INT, b : INT) : INTvariable r : INTbeginwhile b <> 0 dor <- a mod ba <- bb <- rend whilereturn aend functionvariable x : INTvariable y : INTbeginread(x)read(y)write("gcd = ", gcd(x, y), "\n")endend algorithm
Here gcd is a function with its own local variable r, declared between its header and its own begin. x and y are global variables.
Comments
Write a comment with // until the end of the line, or between /* and */ over several lines.
// Greatest common divisor, Euclid's methodr <- a mod b /* remainder of the division */
Names
- Names are case sensitive:
totalandTotalare two different variables. - A name cannot be a keyword such as
begin,iforend. - The checker refuses two declarations with the same name in the same block.