Soplang Logo
Soplang

Functions

Define and call Soplang functions, return values, use type annotations, and create closures.

Functions in Soplang

Functions are reusable blocks of code. Define a function with hawl and return a value with celi.

Define a Function – hawl

A function definition starts with hawl, followed by a name and parameters. Parameters may be untyped or explicitly typed.

define_function.sop
hawl salaan(magac) {
  qor("Asalaamu calaykum, " + magac)
}

Call a Function

Call a function by writing its name followed by arguments in parentheses:

call_function.sop
hawl salaan(magac) {
  qor("Asalaamu calaykum, " + magac)
}
salaan("Aamina")

Return Values – celi

Use celi to return a value from a function.

return_values.sop
hawl labanlaab(x) {
  celi x * 2
}

abn natiijo = labanlaab(5)
qor("Natiijo: " + natiijo)

A function without celi, or with a bare celi, returns maran (null). A bare celi can exit a function early:

early_return.sop
hawl hubi(x) {
  haddii (x < 0) {
      qor("Tiro taban")
      celi
  }
  qor("Tiro togan")
}

hubi(-1)   // Tiro taban
hubi(3)    // Tiro togan

Typed Parameters and Return Types

Place a type before each parameter name, and add : type after ) to annotate the return value. Known type mismatches are rejected during analysis; dynamic values are checked at runtime.

typed_functions.sop
hawl isu_gee(abn a, abn b): abn {
  celi a + b
}

hawl magac_buuxa(qoraal hore, qoraal dambe): qoraal {
  celi hore + " " + dambe
}

qor(isu_gee(2, 3))                  // 5
qor(magac_buuxa("Aamina", "Cali"))  // Aamina Cali

An argument with a known incompatible type is rejected before execution:

typed_functions_error.sop
hawl isu_gee(abn a, abn b): abn { celi a + b }
isu_gee("2", 3)   // Khalad nooc: doodda 1 ee 'isu_gee' waa 'abn', laakiin 'qoraal' ayaa la helay [E022]

Recursion

A function can call itself:

recursion.sop
hawl fiibo(n) {
  haddii (n < 2) {
      celi n
  }
  celi fiibo(n - 1) + fiibo(n - 2)
}

qor(fiibo(10))   // 55

Functions as Values

Functions are values, and nooc() returns "hawl" for them. Store a function in a variable or pass it to a method such as aaddin() or shaandhee(). Use the function's name without quotes or a call's parentheses.

functions_as_values.sop
hawl laban(x) {
  celi x * 2
}

door f = laban
qor(f(21))                     // 42
qor([1, 2, 3].aaddin(laban))   // [2, 4, 6]

Scope

Variables declared inside a function are local. Functions read the current values of global variables and can update them. Parameters and local declarations shadow global variables with the same name.

scope.sop
door tirin = 0

hawl kordhi() {
  tirin = tirin + 1
}

kordhi()
kordhi()
qor(tirin)   // 2

Loops Inside Functions

Functions can contain loops and other control flow.

loops_inside_functions.sop
hawl muujin(liis) {
  kuceli (i 0 ilaa liis.dherer() - 1) {
      qor("Liiska: " + liis[i])
  }
}

Summary

KeywordMeaning
hawlfunction definition
celireturn value

Anonymous Functions and Closures

hawl(...) { ... } creates an anonymous function. It can capture local values from its enclosing function:

closures.sop
hawl samee(factor) {
  celi hawl(x) { celi x * factor }
}

door laban = samee(2)
qor([1, 2, 3].aaddin(laban))   // [2, 4, 6]

Captured local values use snapshot semantics when the closure is created. Global variables are read from their current shared value. Callback methods validate that their argument is callable, including for empty lists. Argument counts are checked for functions, methods, closures, built-ins and constructors.

The compiled call boundary accepts eight ABI values, including implicit receiver or closure environment arguments. Defaults, keyword arguments, variadic user functions and arbitrary callable expressions remain planned work.

On this page