Functions in Depth
Functions in Clean Language are strictly typed — the compiler knows exactly what each function accepts and returns. This makes composition safe: when you call one function's output as another's input, the compiler verifies the types match.
Part of: How Clean works
Functions call other functions freely — build complex behavior from simple pieces:
start:
print(progress_bar(30))
print(progress_bar(75))
print(progress_bar(110))
functions:
integer clamp(integer value, integer min_val, integer max_val)
if value < min_val
return min_val
if value > max_val
return max_val
return value
string progress_bar(integer percent)
integer clamped = clamp(percent, 0, 100)
integer bars = clamped / 10
string filled = ""
iterate i in 0 to bars - 1
filled = filled + "#"
string empty = ""
iterate i in 0 to (10 - bars) - 1
empty = empty + "-"
return "[" + filled + empty + "] " + clamped.toString() + "%"[###-------] 30%
[#######---] 75%
[##########] 100%progress_bar calls clamp to enforce safe bounds, then builds the bar string. The typed parameters mean calling clamp with a string argument is a compile error. Multiple returns in different branches are fine — whichever branch executes returns from the function.
Functions that return boolean answer a yes/no question — perfect for if conditions:
start:
print(check_email("[email protected]"))
print(check_email("notanemail"))
print(check_email("short@x"))
functions:
boolean is_valid_email(string email)
if email.length() < 5
return false
if email.contains("@") == false
return false
if email.contains(".") == false
return false
return true
string check_email(string email)
if is_valid_email(email)
return email + " looks valid"
return email + " is not a valid email"[email protected] looks valid
notanemail is not a valid email
short@x is not a valid emailis_valid_email returns boolean, so check_email can use it directly as an if condition. This is Clean Language's idiom for validation: extract the check into a boolean function, then call it where needed. The return type makes the function's intent obvious.
- Functions call other functions freely — compose behavior from simple, testable pieces
- Multiple return statements in different branches are allowed
- Boolean functions answer yes/no questions and work directly as if conditions
- Typed parameters are enforced at compile time — wrong argument types are caught early