Skip to content
العربية

8. Functions

To write a function with parameters and a return type, and to call it after defining it.

  • A function is a named group of instructions. It starts with a line such as داله مساحة(ط كسر64, ع كسر64) كسر64:, followed by its body indented underneath.
  • Between the parentheses are the parameters: the name and type of each value the function takes. After the parentheses comes the return type: the type of what it returns. A function that returns no value has the return type فراغ (void).
  • ارجع (return) ends the function and returns the value written after it.
  • A function is called after its definition in the file.
Example
داله مساحة(ط كسر64, ع كسر64) كسر64:
ارجع ط * ع
اطبع(مساحة(2.5, 4.0))
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
10.0

Try this example in the playground

Calling a function before defining it:

Causes the error
اطبع(ضعف(2))
داله ضعف(س عدد64) عدد64:
ارجع س * 2
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
​[خطأ دلالي E0125] سطر 1:6 - الدالة أو الهيكل 'ضعف' غير معرّف   1 | اطبع(ضعف(2))     |      ^~~  تلميح: هل تقصد 'اضف'؟

Exit code: 2

In the error catalog: E0125

Try this example in the playground

Fixes it
داله ضعف(س عدد64) عدد64:
ارجع س * 2
اطبع(ضعف(2))
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
4

Try this example in the playground

And a function with a path that ends without ارجع:

Causes the error
داله اشارة(س عدد64) نص:
اذا س > 0:
ارجع "موجب"
اطبع(اشارة(-1))
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
​[خطأ دلالي E0266] سطر 1:1 - الدالة 'اشارة' قد تنتهي دون إرجاع قيمة   1 | داله اشارة(س عدد64) نص:     | ^~~~~  تلميح: أضف 'ارجع قيمة' في نهاية الدالة وفي كل مسار من مسارات الشروط.

Exit code: 2

In the error catalog: E0266

Try this example in the playground

Fixes it
داله اشارة(س عدد64) نص:
اذا س > 0:
ارجع "موجب"
ارجع "غير موجب"
اطبع(اشارة(-1))
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
غير موجب

Try this example in the playground

Write a function اولي(ن عدد64) منطقي that returns صواب (true) if ن is prime, and use it to print the primes up to 30.

Show the solution
Solution
داله اولي(ن عدد64) منطقي:
اذا ن < 2:
ارجع خطاء
ق = 2
طالما ق * ق <= ن:
اذا ن % ق == 0:
ارجع خطاء
ق += 1
ارجع صواب
لكل ع في مدى(1, 31):
اذا اولي(ع):
اطبع(ع)
Output recorded from the compilermeem 08_حل.م · meem 0.17.3+434e13f
2357111317192329

Functions