Functions
Define reusable logic with parameters, return values, recursion, and closures.
Theory
Functions are reusable blocks of logic that improve readability, reduce duplication, and make code easier to test. In PriyoScript, functions are declared with lisaaTask and optionally return values using priyoGiveBack. A function can accept parameters, perform calculations, call other functions, and return output to the caller. Functions are essential for structuring medium and large programs.
Why functions matter
If a logic block is repeated, move it into a function once and call it where needed.
Function Types Table
| Function type | Syntax pattern | Description |
|---|---|---|
| Basic function (no params) | lisaaTask name() { ... } | Runs reusable logic without input parameters. |
| Function with parameters | lisaaTask name(a, b) { ... } | Accepts input values from caller. |
| Function returning value | priyoGiveBack value | Sends computed result back to caller. |
| Void-style function | no priyoGiveBack | Performs side effects (for example output/logging). |
| Recursive function | name(...) inside same function | Calls itself until base condition is met. |
| Closure function | returns inner function | Inner function remembers outer variables. |
| Async function (stage-1) | prakritiWait lisaaTask ... | Enables prakritiPause inside the function body. |
| Generator-yield (stage-1) | prakritiGiveSome value | Produces step values consumable via next(). |
| Structured concurrency | priyoConcurrency.group(...) | Runs async tasks with group wait/cancel semantics. |
Syntax Quick Reference
monalisa {
lisaaTask add(a, b) {
priyoGiveBack a + b
}
priyoTell(add(2, 3))
}Async Functions and Await (Stage-1)
prakritiWait marks a function as async-capable. prakritiPause waits for an expression to resolve.
Example 1
monalisa {
prakritiWait lisaaTask addAsync(a, b) {
priyoGiveBack prakritiPause (a + b)
}
priyoTell(prakritiPause addAsync(20, 22))
}Output:
42Example 2
monalisa {
prakritiWait lisaaTask greetLater(name) {
priyoGiveBack "Hi " + prakritiPause name
}
priyoTell(prakritiPause greetLater("mona"))
}Output:
Hi monaStructured Concurrency (Task Groups, Cancellation, Scheduling)
priyoConcurrency extends async flow with grouped tasks, cooperative cancellation, and delayed
task scheduling. A task group keeps related work together so you can wait for all tasks, cancel
the whole unit, and inspect task state in one place.
Example 1: Run tasks in a group
monalisa {
prakritiWait lisaaTask fetchName(name) {
prakritiPause priyoConcurrency.after(5, priyoEmpty)
priyoGiveBack name + " done"
}
priyoKeep group = priyoConcurrency.group("names")
priyoKeep first = group.run(fetchName, "mona")
priyoKeep second = group.run(fetchName, "piue")
priyoTell(priyoArray.length(prakritiPause group.all()))
priyoTell(prakritiPause first.join())
}Output:
2
mona doneExample 2: Cooperative cancellation
monalisa {
prakritiWait lisaaTask worker(name, token) {
prakritiPause priyoConcurrency.after(5, priyoEmpty)
token.throwIfCancelled()
priyoGiveBack name + " finished"
}
priyoKeep group = priyoConcurrency.group("cancel")
priyoKeep token = group.token()
priyoKeep task = group.run(worker, "mona", token)
group.cancel("stop now")
prakritiTry {
priyoTell(prakritiPause task.join())
} prakritiCatch (err) {
priyoTell(err.code)
}
}Output:
PRUN-112Example 3: Delayed scheduling
monalisa {
prakritiWait lisaaTask greet(name) {
priyoGiveBack "Hello " + name
}
priyoKeep group = priyoConcurrency.group("scheduled")
priyoKeep task = group.schedule(5, greet, "piue")
priyoTell(task.status())
priyoTell(prakritiPause task.join())
priyoTell(task.status())
}Output:
scheduled
Hello piue
fulfilled1. Function Without Return Value
Runs logic without explicit returned result.
Example 1
monalisa {
lisaaTask greet() {
priyoTell("Hello from function")
}
greet()
}Output:
Hello from functionExample 2
monalisa {
lisaaTask banner() {
priyoTell("==== PriyoScript ====")
}
banner()
}Output:
==== PriyoScript ====2. Function With Return Value
Returns data so caller can reuse the computed value.
Example 1
monalisa {
lisaaTask add(a, b) {
priyoGiveBack a + b
}
priyoTell(add(10, 5))
}Output:
15Example 2
monalisa {
lisaaTask isAdult(age) {
priyoGiveBack age >= 18
}
priyoTell(isAdult(21))
}Output:
priyoTrue3. Recursive Function
A function that calls itself with a smaller problem until base condition.
Example 1
monalisa {
lisaaTask factorial(n) {
prakritiIf (n <= 1) {
priyoGiveBack 1
}
priyoGiveBack n * factorial(n - 1)
}
priyoTell(factorial(5))
}Output:
120Example 2
monalisa {
lisaaTask countdown(n) {
prakritiIf (n == 0) {
priyoGiveBack "done"
}
priyoTell(n)
priyoGiveBack countdown(n - 1)
}
priyoTell(countdown(3))
}Output:
3
2
1
done4. Closure Function
Function returns another function that keeps access to outer variables.
Example 1
monalisa {
lisaaTask makeCounter() {
priyoChange count = 0
lisaaTask next() {
count = count + 1
priyoGiveBack count
}
priyoGiveBack next
}
priyoKeep c = makeCounter()
priyoTell(c())
priyoTell(c())
}Output:
1
2Example 2
monalisa {
lisaaTask makeGreeter(prefix) {
lisaaTask greet(name) {
priyoGiveBack prefix + " " + name
}
priyoGiveBack greet
}
priyoKeep welcome = makeGreeter("Welcome")
priyoTell(welcome("mona"))
}Output:
Welcome mona5. Generator Yield (Stage-1)
prakritiGiveSome emits sequence values from a function. A yield function returns a generator-like
object with next(), hasNext(), and reset().
Example 1
monalisa {
lisaaTask series() {
prakritiGiveSome 1
prakritiGiveSome 2
}
priyoKeep g = series()
priyoTell(g.next().value)
priyoTell(g.next().value)
priyoTell(g.next().done)
}Output:
1
2
priyoTrueExample 2
monalisa {
lisaaTask names() {
prakritiGiveSome "mona"
prakritiGiveSome "piue"
}
priyoKeep g = names()
priyoTell(g.hasNext())
priyoTell(g.next().value)
g.reset()
priyoTell(g.next().value)
}Output:
priyoTrue
mona
monaPractice Questions
Concept Check
- When should you return a value instead of printing inside the function? 2. Why is a base condition mandatory in recursive functions? 3. What value does a closure preserve after the outer function finishes?
Related Docs
Last updated on