01The language
Functions
How to declare a function, what its parameters and result mean, how a call passes values, and the errors a header or a call can cause.
A function is a named group of statements. This page states the whole rules: the shape of a header, the kinds of parameters and results, the two forms of a call, how values travel into a call, and the errors the compiler reports. For a gentle start, see Functions in the Learn book.
Expected output
၄၉
Declaring a function
လုပ်ဆောင်ချက် <name> သည်
<parameters> လက်ခံပြီး (left out when there are none)
အောက်ပါတို့ကို လုပ်ဆောင်မည်။ (no result)
or
အောက်ပါတို့ကို လုပ်ဆောင်ကာ <kind> ရလဒ် ကို ပြန်ပေးမည်။ (with a result)
<statements>
လုပ်ဆောင်စရာများ ပြီးပါပြီး။
| Part | Rule |
|---|---|
| name | a name like each other name |
| parameters | <kind> <name>, joined with ၊; the last two can be joined with နှင့် |
| closer of the list | ကို after one parameter, တို့ကို after two or more |
| result | a single kind, written before the word ရလဒ် |
| end | ပြီးပါပြီ, ပြီးပါပြီး, ပြီးဆုံးပါပြီ or ပြီးဆုံးပါပြီး, all with လုပ်ဆောင်စရာများ before them |
The line breaks of the header are plain white space. The whole header can stand on one line.
Declaring a function runs nothing. The statements inside run each time the function is called.
Where a function stands
- A function is declared at the top level of the program: not inside a block, a loop, a conditional or another function (ZK0214).
- A call can come before the declaration it names. The order of declarations does not matter, and two functions can call each other.
- Each name is declared once. Zaka has no overloading: a second function with a name in use is ZK0304.
- A call to a name that no function has is ZK0303.
Expected error
error[ZK0214]: functions can be declared at the top level and nowhere else
Parameters
A parameter is a variable that lives inside the function. A call fills it with the value of the matching argument, so it starts with that value and nothing else. Parameters follow the rules of other local variables, with one more rule: the names of one header are distinct (ZK0305).
A parameter can have each kind a variable can have: a number, a true/false value, a character, a list, a pointer. What the call does with the argument depends on that kind; see How values are passed below.
The argument must fit the kind of its parameter, under the rules of assignment: a value that widens without loss converts on its own, and each other difference needs an explicit conversion.
Expected output
၇
Results and return
A function without a result returns nothing. A function with a result has a variable named
ရလဒ် ("result") of the result kind. It starts at the default of the kind (၀ for a
number, မမှန် for a true/false value, ဗလာ for an address), and the call gives back
what it holds when the function ends.
A function ends in one of two manners, and both give the same result:
- The end is reached. The call gives back the current value of
ရလဒ်. - A return statement runs.
<value> ကို ရလဒ်အဖြစ် ပြန်ပေးပါ။gives back the value at once, andပြန်ပေးပါ။alone ends a function that has no result.
There is no "missing return" error. A function with a result that reaches its end without
setting ရလဒ် gives back the default of its kind:
Expected output
၀
What the compiler does check:
| Mistake | Code |
|---|---|
| a value returned from a function without a result | ZK0312 |
ပြန်ပေးပါ။ with no value in a function with a result | ZK0313 |
| a return statement outside each function | ZK0311 |
| a result that is a list | ZK0424 |
| a value that does not fit the result kind | ZK0401 |
Expected error
error[ZK0312]: function "ပြ" returns nothing, but a value is returned
Expected error
error[ZK0313]: function "တစ်" returns isize, but no value is returned
A list cannot be a result. The fix is a parameter that holds the address of the list to fill; see Lists.
Expected error
error[ZK0424]: a function cannot give back a list as its result
Calls
A call has two forms. Both name the function after လုပ်ဆောင်ချက်, and both close the
argument list with တို့ဖြင့် ("with these"), one argument or several. A function with no
parameters has no argument list.
| Form | Written | Is |
|---|---|---|
| statement | လုပ်ဆောင်ချက် <name> ကို <arguments> တို့ဖြင့် လုပ်ဆောင်ပါ။ | a statement; a result, if there is one, is dropped |
| expression | လုပ်ဆောင်ချက် <name> ကို <arguments> တို့ဖြင့် လုပ်ဆောင်သော ရလဒ် | a value |
The expression form is an operand like a number: print it, store it, pass it as an argument, use it in a sum, or give it back from another function. Using the expression form of a function that has no result is ZK0315. The number of arguments must match the number of parameters (ZK0314), and each argument must fit its parameter (ZK0401).
An argument is a full expression, and ၊ ends it, so an argument needs no parentheses:
Expected output
၂
၁၀
A statement that begins with လုပ်ဆောင်ချက် is read as a call statement. A sum or other
expression that begins with a call is written with that call in parentheses; see
Expressions.
Expected error
error[ZK0314]: function "ပြ" takes 1 arguments, 2 given
Expected error
error[ZK0401]: expected isize, found bool
How values are passed
Each parameter is a new variable, and the call stores the argument in it. What that store does depends on the kind of the parameter, as it does for a variable set from another:
| Kind of the parameter | What the call does | Can the function change the caller's value? |
|---|---|---|
| number, true/false value, character | duplicates the value | no |
| list | duplicates each cell | no; pass the address of the list to change it |
ငှားထားသော … လိပ်စာ (a borrow) | duplicates the address; the caller keeps the owner | through the address |
… လိပ်စာ (an owner) | moves the block to the function; the caller's variable holds ဗလာ afterwards | the function owns the block now |
… လိပ်စာအကြမ်း (raw) | duplicates the address | through the address, and nothing is tracked |
Values and lists are duplicated
A change to a parameter remains in the function:
Expected output
၆
၅
A list parameter is the same: the call duplicates the list one cell after another, and a large list
costs a duplicate on each call. A function that must change the caller's list, or that
should be spared the cost of a duplicate, takes the address of the list. The parameter kind is then
the list kind with လိပ်စာ after it, and ငှားထားသော in front:
Expected output
[၂၊ ၄၊ ၆]
Addresses give the function a route in
A parameter that borrows a variable lets the function read and write that variable. The
caller passes x ၏ လိပ်စာ, and the function writes through the borrow. This is also the
route to get more than one result out of a call:
Expected output
၂
၁
The variables of a function end with the call, so a function never gives back a borrow of one of its own variables: that is ZK0706, in Ownership and borrowing.
Blocks are handed over, lent or created
For a pointer to a block of the heap, the header of the function tells what happens to the block, and the call must agree:
| In the header | The call |
|---|---|
parameter ငှားထားသော ကိန်းပြည့် လိပ်စာ ပ | lends the block: write ပ ၏ တန်ဖိုးအငှား; the caller keeps it |
parameter ကိန်းပြည့် လိပ်စာ ပ | hands the block over: the caller's variable holds ဗလာ afterwards |
result ကိန်းပြည့် လိပ်စာ ရလဒ် | the function made a new block, and the caller owns it |
result ငှားထားသော ကိန်းပြည့် လိပ်စာ ရလဒ် | a borrow into a block that the caller lent, or at a top-level variable |
The full rules, with one error for each manner of getting them wrong, are in Ownership and borrowing.
Recursion
A function can call itself, at once or through other functions. Nothing marks a function
as recursive; the call is a plain call. Each call has its own parameters and its own
ရလဒ်.
Expected output
မှန်
မမှန်
The VM allows 4096 calls that wait to return at one time; the next call stops the
program. zakac run --max-depth <calls> sets another limit. A recursion without a base
case meets this limit:
Expected output
the program stopped: more than 4096 nested calls
See When a program stops for the other stops.
Errors at a glance
| Code | Mistake |
|---|---|
| ZK0214 | a function declared inside a block or another function |
| ZK0303 | a call to a function that is not declared |
| ZK0304 | two functions with one name |
| ZK0305 | one parameter name listed twice |
| ZK0311 | a return statement outside a function |
| ZK0312 | a value returned from a function without a result |
| ZK0313 | no value returned from a function with a result |
| ZK0314 | a wrong number of arguments |
| ZK0315 | the result of a function that has none, used as a value |
| ZK0401 | an argument or a returned value that does not fit its kind |
| ZK0424 | a list as a result |
The text of each message is in Error messages.