PriyoScriptPriyoScript
Basics

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 typeSyntax patternDescription
Basic function (no params)lisaaTask name() { ... }Runs reusable logic without input parameters.
Function with parameterslisaaTask name(a, b) { ... }Accepts input values from caller.
Function returning valuepriyoGiveBack valueSends computed result back to caller.
Void-style functionno priyoGiveBackPerforms side effects (for example output/logging).
Recursive functionname(...) inside same functionCalls itself until base condition is met.
Closure functionreturns inner functionInner function remembers outer variables.
Async function (stage-1)prakritiWait lisaaTask ...Enables prakritiPause inside the function body.
Generator-yield (stage-1)prakritiGiveSome valueProduces step values consumable via next().
Structured concurrencypriyoConcurrency.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:

42

Example 2

monalisa {
  prakritiWait lisaaTask greetLater(name) {
    priyoGiveBack "Hi " + prakritiPause name
  }

  priyoTell(prakritiPause greetLater("mona"))
}

Output:

Hi mona

Structured 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 done

Example 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-112

Example 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
fulfilled

1. Function Without Return Value

Runs logic without explicit returned result.

Example 1

monalisa {
  lisaaTask greet() {
    priyoTell("Hello from function")
  }

  greet()
}

Output:

Hello from function

Example 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:

15

Example 2

monalisa {
  lisaaTask isAdult(age) {
    priyoGiveBack age >= 18
  }

  priyoTell(isAdult(21))
}

Output:

priyoTrue

3. 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:

120

Example 2

monalisa {
  lisaaTask countdown(n) {
    prakritiIf (n == 0) {
      priyoGiveBack "done"
    }
    priyoTell(n)
    priyoGiveBack countdown(n - 1)
  }

  priyoTell(countdown(3))
}

Output:

3
2
1
done

4. 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
2

Example 2

monalisa {
  lisaaTask makeGreeter(prefix) {
    lisaaTask greet(name) {
      priyoGiveBack prefix + " " + name
    }
    priyoGiveBack greet
  }

  priyoKeep welcome = makeGreeter("Welcome")
  priyoTell(welcome("mona"))
}

Output:

Welcome mona

5. 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
priyoTrue

Example 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
mona

Practice Questions

Concept Check

  1. 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?

Last updated on

On this page