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.

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 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…

The EK9 IDE once this is done: 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 EK9 IDE just before choosing Preview: square is the function's name

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

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

Select the square() as pure line and, above it, type the comment the book puts there. Saves when typed.

The EK9 IDE once this is done: Select the square() as pure line and, above it, type the comment the book puts there

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.

The EK9 IDE once this is done: 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

Step 2: add — a pure function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: 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 EK9 IDE just before choosing Preview: add is the function's name

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

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

Select the add() as pure line and, above it, type the comment the book puts there. Saves when typed.

The EK9 IDE once this is done: Select the add() as pure line and, above it, type the comment the book puts there

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.

The EK9 IDE once this is done: 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

Step 3: sumOfSquares — a pure function

Choose Code > New Construct > Function…

The EK9 IDE once this is done: 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 EK9 IDE just before choosing Preview: sumOfSquares is the function's name

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

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

Select the sumOfSquares() as pure line and, above it, type the comment the book puts there. Saves when typed.

The EK9 IDE once this is done: Select the sumOfSquares() as pure line and, above it, type the comment the book puts there

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.

The EK9 IDE once this is done: 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

Step 4: The program

Choose Code > New Construct > Program…

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

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

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

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

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

Select the Demo() 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 Demo() line and, above it, type the blank line the book opens the program with

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.

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

Step 5: Run it

Choose Run > Run Program…

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

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

The EK9 IDE just before choosing Run: 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 EK9 IDE just before choosing Close: square of 5 is 25, 5 + 4 is 9, sum of squares of 5 and 3 is 34

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.

The EK9 IDE when the lesson has finished

Where this lesson comes from