Chapter 4: three pure functions, built
Chapter 4's Functions builds three pure functions - one parameter, two parameters, and one that calls the others - and a program that prints what each gives. The IDE writes every function's signature, purity ticked in the dialog, and the program's shell; you type the answers and the comments. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-functions-demo.
What you'll build
The lesson builds this program from a file holding only its module line - the finished file, as the book prints it.
#!ek9 defines module functions.demo defines function //One parameter in, one value out. square() as pure -> n as Integer <- result as Integer: n * n //Two parameters. Give the incoming block once, then list them. add() as pure -> a as Integer b as Integer <- total as Integer: a + b //A function can call other functions. sumOfSquares() as pure -> a as Integer b as Integer <- total as Integer: add(square(a), square(b)) defines program Demo() -> n as Integer stdout <- Stdout() stdout.println(`square of ${n} is ${square(n)}`) stdout.println(`${n} + 4 is ${add(n, 4)}`) stdout.println(`sum of squares of ${n} and 3 is ${sumOfSquares(n, 3)}`) //EOF
Step 1: square — a pure function
Choose Code > New Construct > Function…
square is the function's name. Add n of type Integer as its parameter, set Returns to Integer and tick pure. Choose Preview.
The preview shows square about to be written. Choose Apply.
Select the square() as pure line and, above it, type the comment the book puts there. Saves when typed.
Select the stub, from <- rtn as Integer? down to its //AI line, and type square's own return and work, as the book writes them. Saves when typed.
Step 2: add — a pure function
Choose Code > New Construct > Function…
add is the function's name. Add a of type Integer, b of type Integer - two parameters, set Returns to Integer and tick pure. Choose Preview.
The preview shows add about to be written. Choose Apply.
Select the add() as pure line and, above it, type the comment the book puts there. Saves when typed.
Select the stub, from <- rtn as Integer? down to its //AI line, and type add's own return and work, as the book writes them. Saves when typed.
Step 3: sumOfSquares — a pure function
Choose Code > New Construct > Function…
sumOfSquares is the function's name. Add a of type Integer, b of type Integer - two parameters, set Returns to Integer and tick pure. Choose Preview.
The preview shows sumOfSquares about to be written. Choose Apply.
Select the sumOfSquares() as pure line and, above it, type the comment the book puts there. Saves when typed.
Select the stub, from <- rtn as Integer? down to its //AI line, and type sumOfSquares's own return and work, as the book writes them. Saves when typed.
Step 4: The program
Choose Code > New Construct > Program…
Demo is the program's name. Add one parameter, n of type Integer. Choose Preview.
The preview shows Demo about to be written. Choose Apply.
Select the Demo() line and, above it, type the blank line the book opens the program with. Saves when typed.
Select the stub, from require n? down to its //AI line, and type Demo's body exactly as the chapter shows it. Saves when typed.
Step 5: Run it
Choose Run > Run Program…
Type 5 into Arguments (space-separated), then choose Run.
square of 5 is 25, 5 + 4 is 9, sum of squares of 5 and 3 is 34. Choose Close.
The result
Functions.ek9 now reads exactly as chapter 4 of Getting Started with EK9 shows it: square, add and sumOfSquares, each pure, the last calling the first two. The signatures were the tool's; the answers are yours.
Where this lesson comes from
- For AI assistants - the MCP tool
ek9_new_construct,ek9_add_program.
In the EK9 reference: Functions in EK9
























