Skip to content

Functions

Functions in Myst are expressions that can be used anywhere in your code. They are defined using the လုပ်ဆောင်ချက် keyword.

Basic Function Syntax

myst
function_name သည် လုပ်ဆောင်ချက်(parameter1၊ parameter2) {
    // function body
    ရလဒ် expression
}။

Functions are expressions

Functions are expressions, which means they can be:

  • Assigned to variables
  • Passed as arguments to other functions
  • Returned from other functions
  • Used in any context where an expression is expected

Example:

myst
နှစ်ဆ သည် လုပ်ဆောင်ချက်(ဂဏန်း) {
    ရလဒ် ဂဏန်း * ၂
}။

အဖြေ = နှစ်ဆ(၇) // function call as expression
ထုတ်ပြ("အဖြေ: " + အဖြေ) // output => အဖြေ: ၁၄

Function Parameters

Functions can have multiple parameters, and the arguments can be any valid expression when calling the function.

Example:

myst
ပေါင်းလုပ်ဆောင်ချက် သည် လုပ်ဆောင်ချက်(က၊ ခ) {
    ရလဒ် က + ခ
}။

ထုတ်ပြ(ပေါင်းလုပ်ဆောင်ချက်(၅၊ ၃)) // output => ၈
ထုတ်ပြ(ပေါင်းလုပ်ဆောင်ချက်("မင်္ဂလာ"၊ "ပါ")) // output => မင်္ဂလာပါ

Return Statement (ရလဒ်)

The ရလဒ် statement is optional. If you don't use ရလဒ်, the function will automatically return the value of the last expression in the function body.

Example without explicit return:

myst
နှစ်ဆ သည် လုပ်ဆောင်ချက်(ဂဏန်း) {
    ဂဏန်း * ၂ // This will be returned automatically
}။

ထုတ်ပြ(နှစ်ဆ(၅)) // output => ၁၀

Example with explicit return:

myst
နှစ်ဆ သည် လုပ်ဆောင်ချက်(ဂဏန်း) {
    တကယ်လို့ (ဂဏန်း < ၀) ဖြစ်ခဲ့လျှင် {
        ရလဒ် ၀ // Early return for negative numbers
    }
    ရလဒ် ဂဏန်း * ၂ // Return for positive numbers
}။

ထုတ်ပြ(နှစ်ဆ(-၃)) // output => ၀
ထုတ်ပြ(နှစ်ဆ(၄)) // output => ၈

Function as Arguments

Since functions are expressions, they can be passed as arguments to other functions.

Example:

myst
လုပ်ဆောင်ချက်၁ သည် လုပ်ဆောင်ချက်(ဂဏန်း) {
    ရလဒ် ဂဏန်း + ၁
}။

လုပ်ဆောင်ချက်၂ သည် လုပ်ဆောင်ချက်(ဂဏန်း) {
    ရလဒ် ဂဏန်း * ၂
}။

အသုံးပြုလုပ်ဆောင်ချက် သည် လုပ်ဆောင်ချက်(လုပ်ဆောင်ချက်နာမည်၊ တန်ဖိုး) {
    ရလဒ် လုပ်ဆောင်ချက်နာမည်(တန်ဖိုး)။
}။

ထုတ်ပြ(အသုံးပြုလုပ်ဆောင်ချက်(လုပ်ဆောင်ချက်၁၊ ၅)) // output => ၆
ထုတ်ပြ(အသုံးပြုလုပ်ဆောင်ချက်(လုပ်ဆောင်ချက်၂၊ ၅)) // output => ၁၀

Closures

Functions can capture variables from their surrounding scope, creating closures. This allows functions to "remember" the environment in which they were created, even after the outer function has finished executing.

A common closure pattern is a function that defines a variable and returns another function that uses that variable. The returned function can then be executed later, and it will still have access to the variable from the outer function.

Example:

myst
မြှောက်လုပ်ဆောင်ချက် သည် လုပ်ဆောင်ချက်(မြှောက်ကိန်း) {
    လုပ်ဆောင်ချက်(တန်ဖိုး) {
        ရလဒ် တန်ဖိုး * မြှောက်ကိန်း
    }
}။

သုံးဆ = မြှောက်လုပ်ဆောင်ချက်(၃)
ထုတ်ပြ(သုံးဆ(၄)) // output => ၁၂

ငါးဆ = မြှောက်လုပ်ဆောင်ချက်(၅)
ထုတ်ပြ(ငါးဆ(၂)) // output => ၁၀

In this example, မြှောက်လုပ်ဆောင်ချက် returns a new function that "remembers" the value of မြှောက်ကိန်း from when it was created. Each returned function is a closure with its own captured environment.

Nested Functions

Functions can be defined inside other functions, creating nested function definitions.

Example:

myst
အပြင်လုပ်ဆောင်ချက် သည် လုပ်ဆောင်ချက်(ဂဏန်း) {
    အတွင်းလုပ်ဆောင်ချက် သည် လုပ်ဆောင်ချက်(အပေါင်း) {
        ရလဒ် ဂဏန်း + အပေါင်း
    }။

    ရလဒ် အတွင်းလုပ်ဆောင်ချက်(၁၀)
}။

ထုတ်ပြ(အပြင်လုပ်ဆောင်ချက်(၅)) // output => ၁၅

Built-in Functions

Myst provides several built-in functions:

  • ထုတ်ပြ(...) — Output/print value(s)
  • အရေအတွက်(...) — Count length of string or array
  • ထည့်သွင်း(array၊ value) — Append value to array
  • ထိပ်ဆုံး(array) — Get first element of array
  • နောက်ဆုံး(array) — Get last element of array
  • နောက်အကျန်(array) — Get all except first element

Example:

myst
နာမည်များ = ["မောင်မောင်"၊ "အေးအေး"၊ "ထွန်းထွန်း"]

// Output/print
ထုတ်ပြ("မင်္ဂလာပါ") // output => မင်္ဂလာပါ
ထုတ်ပြ("နာမည်: " + နာမည်များ[၀]) // output => "နာမည်: မောင်မောင်"

// Count length
ထုတ်ပြ("အရေအတွက်: " + အရေအတွက်(နာမည်များ)) // output => "အရေအတွက်: ၃"
ထုတ်ပြ("စာလုံးအရေအတွက်: " + အရေအတွက်("မောင်မောင်")) // output => "စာလုံးအရေအတွက်: ၅"

// Append value
နာမည်များ = ထည့်သွင်း(နာမည်များ၊ "ဇော်ဇော်")
ထုတ်ပြ(နာမည်များ) // output => "["မောင်မောင်"၊ "အေးအေး"၊ "ထွန်းထွန်း"၊ "ဇော်ဇော်"]"

// First element
ထုတ်ပြ("ထိပ်ဆုံး: " + ထိပ်ဆုံး(နာမည်များ)) // output => "ထိပ်ဆုံး: မောင်မောင်"

// Last element
ထုတ်ပြ("နောက်ဆုံး: " + နောက်ဆုံး(နာမည်များ)) // output => "နောက်ဆုံး: ဇော်ဇော်"

// All except first element
ထုတ်ပြ("နောက်အကျန်: " + နောက်အကျန်(နာမည်များ)) // output => "နောက်အကျန်: ["အေးအေး"၊ "ထွန်းထွန်း"၊ "ဇော်ဇော်"]"

Summary

  • Functions in Myst are expressions: they can be assigned to variables, passed as arguments, returned from other functions, and used anywhere an expression is allowed.
  • Functions are defined with the လုပ်ဆောင်ချက် keyword and can have multiple parameters.
  • The ရလဒ် (return) statement is optional. If omitted, the last line of the function body is returned automatically. Use ရလဒ် to return early or explicitly.
  • Functions can capture variables from their surrounding scope, creating closures.
  • Functions can be nested inside other functions.
  • Myst provides several built-in functions for output, array manipulation, and more.