Skip to content
Xen / docs

Search documentation

English · v0.1.0-demo.2

Type to search the documentation.

    한국어
    Xen / coding-class v0.1.0-demo.2

    Functions and control flow · Coding Class

    Apply the chapter in three steps: an exercise, a program project, and a debugging task. Implement first, then consult the hints and completion criteria. Use a separate file or working directory for each task.

    Sum 1..5, compare with 1..6, and also run 5..5.

    Completion: The totals are 10, 15, and 0.

    Hint: The endpoint is excluded.

    2. Build a program · Build an even-number total

    Section titled “2. Build a program · Build an even-number total”

    Write sum_even(limit: Int) -> Int to sum even numbers from zero up to, but excluding, limit. Print calls with 7, 1, and 0.

    Completion: Output is 12, 0, 0. Keep calculation in the function and calls/output in main.

    Hint: Select even numbers with number % 2 == 0.

    The positive function below does not return for every input. Make all paths return a Bool.

    Start with this deliberately broken program.

    class_return.xen
    fn positive(value: Int) -> Bool {
    if value > 0 { return true; }
    }
    fn main() { println(positive(0)); }
    Terminal window
    xen check class_return.xen

    Diagnostic excerpt:

    class_return.stderr
    function 'positive' may fall through without returning a value

    Completion: Inputs 1, 0, and -1 yield true, false, and false.

    Hint: Return false after the if, or return from an else branch.

    Use xen check file.xen followed by xen run file.xen for ordinary programs; use xen test file.xen for test-only files. Record the original error, its cause, and the corrected result, then rerun every input in the completion criteria.