Chapter 4: a function, built

Chapter 4 meets a function with nothing to return: announce, which prints a greeting and hands nothing back, and the program that calls it twice. Leaving out a function's '<-' line is how EK9 says "returns nothing" - there is no separate keyword for it. The IDE's own tools make the structure - the function's whole signature, the program's shell - so what is left to type is the logic no tool can write, and the short line explaining why there is no return. To play it in the IDE: Help > Lessons > Writing code…, or ek9 -ide -lesson book-functions.

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

  defines function

    //No '<-' line: this function does its work and returns nothing.
    announce()
      -> name as String
      stdout <- Stdout()
      stdout.println(`Hello, ${name}`)

  defines program

    Demo()
      announce("Ada")
      announce("Grace")

//EOF

Step 1: A function

Choose Code > New Construct > Function…

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

announce is the function's name - doSomething hints that a function's name should say what it does. Add one parameter, name as String, and choose Preview.

The EK9 IDE just before choosing Preview: announce is the function's name - doSomething hints that a function's name should say what it does

The preview shows announce about to be written, with its one parameter - no return line at all. Choose Apply.

The EK9 IDE just before choosing Apply: The preview shows announce about to be written, with its one parameter - no return line at all

Step 2: Its comment, and its logic

Select the announce() line, and above it type a blank line and //No '<-' line: this function does its work and returns nothing. - why there is no '<-' line. Saves when typed.

The EK9 IDE once this is done: Select the announce() line, and above it type a blank line and //No '<-' line

Select the stub, from require name? down to its //AI line, and type announce's whole job: a Stdout, and a line printing the greeting. Saves when typed.

The EK9 IDE once this is done: Select the stub, from require name? down to its //AI line, and type announce's whole job

Step 3: 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 - no application to bind, no parameters, nothing injected. Type Demo and choose Preview.

The EK9 IDE just before choosing Preview: Demo is the program's name - no application to bind, no parameters, nothing injected

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 every chapter example opens its first member with. Saves when typed.

The EK9 IDE once this is done: Select the Demo() line, and above it type the blank line every chapter example opens its first member with

Select the stub, from ready <- true down to its //AI line, and type Demo's body: two calls to announce, with no value to use each is a line on its own. Saves when typed.

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

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.

Hello, Ada then Hello, Grace - announce ran once for each name Demo called it with. Choose Close.

The EK9 IDE just before choosing Close: Hello, Ada then Hello, Grace - announce ran once for each name Demo called it with

The result

Announce.ek9 now reads exactly as chapter 4 of Getting Started with EK9 shows it: a function with no return value - leaving out the '<-' line says it - built by the tools, its logic and the teaching line above it typed. The same two tools - ek9_new_construct and ek9_add_program - are what an AI assistant calls to do the same thing.

The EK9 IDE when the lesson has finished

Where this lesson comes from