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.

The EK9 IDE as the lesson opens

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…

The EK9 IDE once this is done: Choose Code > New Construct > Program…

ReturnedExit is the program's name. Add one parameter, rows of type Integer. Choose Preview.

The EK9 IDE just before choosing Preview: ReturnedExit is the program's name

The preview shows ReturnedExit about to be written. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows ReturnedExit about to be written

Select the ReturnedExit() line and, above it, type the blank line the book opens the program with. Saves when typed.

The EK9 IDE once this is done: Select the ReturnedExit() line and, above it, type the blank line the book opens the program with

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.

The EK9 IDE once this is done: Select the stub, from require rows? down to its //AI line, and type ReturnedExit's body exactly as the chapter shows it

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.

The EK9 IDE once this is done: New Program has no field for a return, so type it

Step 2: Run it

Choose Run > Run Program…

The EK9 IDE once this is done: Choose Run > Run Program…

Type 7 into Arguments (space-separated), then choose Run.

The EK9 IDE just before choosing Run: Type 7 into Arguments (space-separated), then choose Run

rows found: 7 - and the process ends with exit status 0. Choose Close.

The EK9 IDE just before choosing Close: rows found

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.

The EK9 IDE when the lesson has finished

Where this lesson comes from