စံကား

07Functions

Parameters

Pass values into a function.

A function becomes far more useful when it receives values to work with. The values a function accepts are its parameters. The values passed in a call are the arguments.

စံကား

Expected output

၁
၂
၃
---
၁
၂

Inside the function, အရေအတွက် is a variable that holds whatever value the call passed in: 3 the first time, 2 the second.

ZakaReads as
ကိန်းပြည့် အရေအတွက် ကို လက်ခံပြီး"accepting an integer amount, and ..."
လက်ခံပြီး"having accepted"
၃ တို့ဖြင့်"with 3"

Several parameters

List parameters with ၊, the Burmese comma. Each has its kind before its name. Close a list of two or more with တို့ကို, and pass the arguments in the same order:

စံကား

Expected output

၁၂
၅၀

As in Burmese, နှင့် ("and") can replace the last ၊: ကိန်းပြည့် က၊ ကိန်းပြည့် ခ နှင့် ကိန်းပြည့် ဂ တို့ကို လက်ခံပြီး.

Number of parametersCloserExample
oneကိုကိန်းပြည့် က ကို လက်ခံပြီး
two or moreတို့ကိုကိန်းပြည့် က၊ ကိန်းပြည့် ခ တို့ကို လက်ခံပြီး

Arguments close with တို့ဖြင့် ("with these") in all cases, even when there is just one.

Parameters are local copies

A parameter is a new variable inside the function. Changing it does not change the variable that was passed in:

စံကား

Expected output

၆
၅

Matching the call to the function

The call must pass the same number of arguments as the function has parameters, and each must fit its parameter's kind:

စံကားZK0314

Expected error

error[ZK0314]: function "ပြ" takes 1 arguments, 2 given

Hands on

  • Write a function that takes a number and prints its times table from 1 to 10.
  • Give ရေတွက်ပြ a guard: အကယ်၍ အရေအတွက် သည် ၁ အောက် ငယ်လျှင် ပြန်ပေးပါ။, then call it with ၀.

Next: Results.