Try It Yourself
Small exercises you can change and run.
These exercises build on the language guide. Try each task before opening its solution. The prompt, hint, and expected result stay visible so you can check your thinking first.
For Exercises 1–5, save the solution using the file name shown, then check and run it. Replace exercise-1.ck with the chosen file name:
macOS or Linux:
./ckc check exercise-1.ck
./ckc run exercise-1.ck
Windows PowerShell:
.\ckc.exe check .\exercise-1.ck
.\ckc.exe run .\exercise-1.ck
Exercises 6 and 7 are reusable library functions that receive data from a calling program. They do not have a main function, so check them with ckc check; the example result describes what the function returns for the stated input.
If you still need to download the compiler or create your first file, follow the quick start.
Exercise 1: Add two numbers#
Try it: Add 12 and 5, then show the result.
Hint: Store the answer in a variable, then print it.
Expected output:
17
Show full solution
Save as: exercise-1.ck
fn main() -> i32 {
let first: i32 = 12;
let second: i32 = 5;
let total: i32 = first + second;
print_i32(total);
print_newline();
return 0;
}
Exercise 2: Change a function#
Try it: The double function multiplies its input by 2. Change it to multiply by 3, then call it with 7.
Start with this function, then add main:
fn double(value: i32) -> i32 {
return value * 2;
}
Hint: Change the multiplication inside the function, then use print_i32(double(7)); in main to show the result.
Expected output:
21
Show full solution
Save as: exercise-2.ck
fn double(value: i32) -> i32 {
return value * 3;
}
fn main() -> i32 {
print_i32(double(7));
print_newline();
return 0;
}
The function keeps its original name, but now multiplies by three. main prints the returned value.
Exercise 3: Check a score#
Try it: Show 1 when a score is at least 60; otherwise, show 0. Run the program with 75, then change the score to 50 and run it again.
Hint: Use if and else to choose which value to print.
Expected output:
1
After changing the score to 50, the output is 0.
Show full solution
Save as: exercise-3.ck
fn main() -> i32 {
let score: i32 = 75;
if score >= 60 {
print_i32(1);
} else {
print_i32(0);
}
print_newline();
return 0;
}
>= means “greater than or equal to.” With 50, the condition is false, so the else branch prints 0.
Exercise 4: Add the numbers from 1 to 5#
Try it: Use a while loop to calculate 1 + 2 + 3 + 4 + 5, then show the total.
Hint: Keep a running total and increase the current number once per loop.
Expected output:
15
Show full solution
Save as: exercise-4.ck
fn main() -> i32 {
let number: i32 = 1;
let total: i32 = 0;
while number <= 5 {
total = total + number;
number = number + 1;
}
print_i32(total);
print_newline();
return 0;
}
Each loop adds the current number to total, then increases number. When it reaches 6, the condition is false and the loop ends.
Advanced exercises#
The next exercises use reusable calculations, loops, and slices. Exercises 6 and 7 are for programs that receive memory from a caller. Read the slice introduction in Memory & Safety first, then use ckc check to check your functions.
Exercise 5: Calculate a factorial#
Try it: Write a function that calculates the factorial of 6. A factorial multiplies the positive whole numbers from 1 through the chosen number: 6! = 1 × 2 × 3 × 4 × 5 × 6.
Hint: Start the product at 1. Multiply it by a counter that increases from 1 to 6.
Expected output:
720
Show full solution
Save as: exercise-5.ck
fn factorial(value: i32) -> i32 {
let factor: i32 = 1;
let product: i32 = 1;
while factor <= value {
product = product * factor;
factor = factor + 1;
}
return product;
}
fn main() -> i32 {
print_i32(factorial(6));
print_newline();
return 0;
}
The loop keeps the intermediate product in one variable. The function can be reused with another small positive whole number; larger results exceed the i32 range.
Exercise 6: Find the largest value in a slice#
Try it: Write a function that receives a non-empty slice of whole numbers and returns its largest value. For the input [6, -2, 14, 9], it should return 14.
Hint: Use the first item as the current largest value, then compare each remaining item with it.
Example result: For [6, -2, 14, 9], the function returns 14.
Show full solution
Save as: exercise-6.ck
export fn largest(items: slice<i32>) -> i32 {
let best: i32 = items[0];
let index: u32 = 1;
while index < items.len {
if items[index] > best {
best = items[index];
}
index = index + 1;
}
return best;
}
This function expects a non-empty slice. The calling program provides its memory; index visits each item once and updates best when it finds a larger value.
Exercise 7: Sum part of a slice#
Try it: Write a function that receives a slice and two indexes, then returns the sum in the half-open range from start up to (but not including) end. For [4, 7, 1, 9, 3] and the range 1..4, the selected values are 7, 1, and 9.
Hint: First make a sub-slice with items[start..end], then use an index to add its values.
Example result: For [4, 7, 1, 9, 3] and 1..4, the function returns 17.
Show full solution
Save as: exercise-7.ck
export fn sum_range(items: slice<i32>, start: u32, end: u32) -> i32 {
let selected: slice<i32> = items[start..end];
let index: u32 = 0;
let total: i32 = 0;
while index < selected.len {
total = total + selected[index];
index = index + 1;
}
return total;
}
The caller must supply indexes satisfying start <= end <= items.len. CK slices are views of memory supplied by the caller; this function does not allocate or copy the input values.
For more on functions and loops, see the language guide. Slices are an advanced topic covered in Memory & Safety.