* Documentation

Examples

Copy-paste programs showcasing common Turf patterns.

Max of two numbers

int a = 10
int b = 20

int max_val = if a > b then a else b
printline(max_val)

Factorial (recursive function)

fn int factorial(int n) {
  if n <= 1 {
    return 1
  } else {
    return n * factorial(n - 1)
  }
}

int result = factorial(5)
printline(result)

Arrays + for loops

This example fills an array with squares and prints them:

int[5] squares

for i in 0..5 step i += 1 {
  squares[i] = i * i
}

for j in 0..5 step j += 1 {
  printline(squares[j])
}

typeof() and lengthof()

int x = 10
double y = 3.14
bool flag = true
string msg = "hello"

print("typeof(x): ")
printline(typeof(x))

print("typeof(y): ")
printline(typeof(y))

print("typeof(flag): ")
printline(typeof(flag))

print("typeof(msg): ")
printline(typeof(msg))

printline(lengthof("Turf"))