Functions are the core units of logic in TechScript.
Functions are declared using the to keyword followed by the function name, parameters, a colon :, and an indented block:
to greet(name):
say "Hello, {name}!"
to add(a, b):
return a + b
result is add(10, 20)
say "Sum: {result}" # Sum: 30
Use return to send a value back to the caller:
to calculate_area(width, height):
return width * height
area is calculate_area(5, 8)
say "Area: {area}"
You can define parameters with fallback values:
to welcome(name, greeting = "Hello"):
say "{greeting}, {name}!"
welcome("Alice") # Hello, Alice!
welcome("Bob", "Hi there") # Hi there, Bob!
Functions can call themselves recursively:
to factorial(n):
if n <= 1:
return 1
return n * factorial(n - 1)
say "Factorial(5): {factorial(5)}" # 120