Chapter 32: a program that returns its exit status, built
A script reads a program's result from its exit status, and in EK9 that status is the program's return: a program that returns an Integer exits with it. This one takes the number of rows found as its argument, prints it, and exits with 2 when there were none. The IDE writes the program's shell, with its parameter; you type the return line and the body. To play it in the IDE: Help > Lessons > EK9 in Depth…, or ek9 -ide -lesson depth-returned-exit.
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 book.exit.returned defines program ReturnedExit() -> rows as Integer <- exitCode as Integer: 0 stdout <- Stdout() stdout.println(`rows found: ${rows}`) exitCode: rows == 0 <- 2 : 0
Step 1: The program
Choose Code > New Construct > Program…
ReturnedExit is the program's name. Add one parameter, rows of type Integer. Choose Preview.
The preview shows ReturnedExit about to be written. Choose Apply.
Select the ReturnedExit() line and, above it, type the blank line the book opens the program with. Saves when typed.
Select the stub, from require rows? down to its //AI line, and type ReturnedExit's body exactly as the chapter shows it. Saves when typed.
New Program has no field for a return, so type it: the line below the parameter declares the exit status, an Integer that starts at 0. Saves when typed.
Step 2: Run it
Choose Run > Run Program…
Type 7 into Arguments (space-separated), then choose Run.
rows found: 7 - and the process ends with exit status 0. Choose Close.
The result
ReturnedExit.ek9 now reads exactly as chapter 32 of EK9 in Depth shows it. Given 0 rows it prints rows found: 0 and exits with status 2; given 7, as the run did, it prints rows found: 7 and exits with 0.
Where this lesson comes from
- Answers -
ek9 -qand the IDE's Answers tab: 1431. - For AI assistants - the MCP tool
ek9_add_program.










