Control flow and functions

Use a function and a loop to compute a result; continue skips one iteration.

A function gives a name to reusable work. Here square returns one number. range(1, 6) produces 1 through 5, and the loop adds each square except 3 because continue skips the rest of that pass. The semicolon ends each statement; braces mark the loop and if bodies.

Script: control-flow-functions.lms

fn square(n) { return n * n; }
total = 0;
for (n in range(1, 6)) {
  if (n == 3) { continue; }
  total = total + square(n);
}
println(f"sum={total}");

Run it

lmscript examples/control-flow-functions.lms

Output

sum=46

Previous: Quick cheat sheet · Next: Strings and data

Try it: download examples/control-flow-functions.lms, save it under examples/ in a writable working directory, then run the command above from that directory.