Home/Documentation/Language Basics

Language Basics

Learn numbers, variables, functions, decisions, and loops.

This page introduces the pieces used in everyday CK programs: values, names, functions, choices, and repetition. Each code example is a complete program you can save as lesson.ck and run with the quick-start commands using lesson.ck as the file name instead of hello.ck.

1. Values have a kind#

A value is a piece of information your program can use. 12 is a number; true is a yes-or-no value. CK gives each value a type, which tells the compiler what kind of information it is and which operations make sense.

For now, remember these three types:

Type What it holds Example
i32 A whole number, including negative numbers -3, 0, 12
u32 A whole number that is zero or greater 0, 12
f64 A number that can have a fractional part 3.5, -0.25

There is also bool, which can be true or false. Most first programs can use i32 for ordinary whole-number calculations.

2. Give a value a name#

Use let to create a named value. Write its name, its type after :, and its starting value after =.

fn main() -> void {
  let apples: i32 = 3;
  apples = apples + 1;
  print_i32(apples);
  print_newline();
}

This program prints 4. apples starts at 3; the next line adds one and stores the new value back in apples. In CK, a name created with let can be given a new value later. The type still has to match: an i32 name must keep holding an i32 value.

3. Put a calculation in a function#

A function is a named set of steps. It can receive values, work with them, and give a result back. This function receives one whole number and returns twice that number:

fn double(value: i32) -> i32 {
  return value * 2;
}

fn main() -> void {
  let result: i32 = double(21);
  print_i32(result);
  print_newline();
}

It prints 42. In double(value: i32), value is the name of the input and i32 is its type. -> i32 says the function returns a whole number. return gives that result back to the place that called the function. The second function calls double with 21 and prints the answer.

main is a special name for a small program's starting function. -> void means there is no result to return. The built-in print_i32 and print_newline commands are available to the main function in a program you run on your computer.

4. Make a choice with if#

An if runs one group of steps when a test is true. An optional else runs when it is false.

fn main() -> void {
  let score: i32 = 12;
  if score >= 10 {
    print_i32(1);
  } else {
    print_i32(0);
  }
  print_newline();
}

This prints 1 because 12 >= 10 is true. The comparison >= means “greater than or equal to.” Other common comparisons are < (less than), > (greater than), and == (equal to).

5. Repeat steps with while#

A while repeats its group of steps for as long as its test stays true. This program prints 1, 2, and 3, each on its own line:

fn main() -> void {
  let count: i32 = 1;
  while count <= 3 {
    print_i32(count);
    print_newline();
    count = count + 1;
  }
}

The program starts count at 1. After printing, it adds one. When count becomes 4, count <= 3 is false and the loop ends. Changing a value inside the loop is what lets this example finish; without that change, the test would keep seeing the same value.

A few useful rules#

  • CK checks that values have the types a function expects. It will not silently mix different number types for you.
  • +, -, *, and / do arithmetic. Comparisons such as < and == produce a bool value that can be used by if and while.
  • Put ; after a value declaration, an assignment, a return, or a command such as print_i32(...). Braces group the steps controlled by a function, if, else, or while.
  • Use .ck as the file extension, then check your work with the matching check command from the quick start before running it.

Where to go next#

Try the small practice exercises and change the examples above. When you want to share calculations with another program, read about export fn and output choices. Pointers, slices, and memory sharing are useful later; the Memory & Safety guide is an advanced topic and is not needed for these first lessons.

For the full list of types, conversions, and language rules, see the CalcKernel language reference.

Repository reference links follow the main branch and may describe features newer than the latest downloadable release.

↵ open · esc close