Chapter 7: a constant that is copied, built

Chapter 7's ConstantCopy shows what a constant promises: handed to a function that changes its parameter, an ordinary variable really changes, but a constant stays as it was - what the function changes is a copy. The IDE writes the constant, the function's signature and the program's shell; you type the body of each. To play it in the IDE: Help > Lessons > Getting Started with EK9…, or ek9 -ide -lesson book-constant-copy.

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.constantcopy

  defines constant
    startingLives <- 3

  defines function

    useOne()
      -> lives as Integer
      lives -= 1

  defines program
    Show()
      stdout <- Stdout()

      myLives <- 3
      useOne(myLives)
      stdout.println(`my variable is now:  ${myLives}`)

      useOne(startingLives)
      stdout.println(`the constant is still: ${startingLives}`)

Step 1: A constant

Choose Code > New Construct > Constant…

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

startingLives is the constant's name and 3 its value - a literal in the Value field, from which the type is inferred. Type both and choose Preview.

The EK9 IDE just before choosing Preview: startingLives is the constant's name and 3 its value - a literal in the Value field, from which the type is inferred

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

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

Step 2: useOne — a function

Choose Code > New Construct > Function…

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

useOne is the function's name. Add lives of type Integer as its parameter. Choose Preview.

The EK9 IDE just before choosing Preview: useOne is the function's name

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

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

Select the useOne() line and, above it, type the blank line the book opens its first function with. Saves when typed.

The EK9 IDE once this is done: Select the useOne() line and, above it, type the blank line the book opens its first function with

Select the stub, from require lives? down to its //AI line, and type useOne's own return and work, as the book writes them. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require lives? down to its //AI line, and type useOne's own return and work, as the book writes them

Step 3: The program

Choose Code > New Construct > Program…

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

Show is the program's name. Choose Preview.

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

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

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

Select the stub, from ready <- true down to its //AI line, and type Show's body exactly as the chapter shows it. Saves when typed.

The EK9 IDE once this is done: Select the stub, from ready <- true down to its //AI line, and type Show's body exactly as the chapter shows it

Step 4: Run it

Choose Run > Run Program…

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

The project's only program is already chosen. Choose Run.

my variable is now: 2, and the constant is still: 3 - the constant was copied, so useOne changed the copy. Choose Close.

The EK9 IDE just before choosing Close: my variable is now

The result

ConstantCopy.ek9 now reads exactly as chapter 7 of Getting Started with EK9 shows it: useOne takes a life off the variable it is handed, and off nothing at all when handed the constant.

The EK9 IDE when the lesson has finished

Where this lesson comes from