Ahadu 2.0
አማርኛ

Functions

Giving code a name and using it again

When you find yourself writing the same code twice, give it a name and write it once. That is a function — ተግባር.

Open in playground This output was verified at build time
ሰላም አበበ
ሰላም አልማዝ

ተግባር comes first, then the name, then the inputs it accepts in parentheses.

Returning a value

A function can hand a value back with ተመላሽ.

Open in playground This output was verified at build time
7
30

ተመላሽ stops the function immediately.

Open in playground This output was verified at build time
12
9

Why they are worth it

A function gives you three things:

  1. No repetition — write it once, call it many times
  2. A name — ትልቁ(ሀ፣ ለ) says more than three lines of comparison
  3. One place to fix — a mistake is corrected in a single spot

A function that calls itself

A function may call itself. This is called recursion.

Open in playground This output was verified at build time
120

ፋክቶሪያል(5) becomes 5 * ፋክቶሪያል(4), then 5 * 4 * ፋክቶሪያል(3), down to

  1. That stopping condition is what makes it finish; without one it never would.

Input from the person running it

አንብብ reads a line from whoever is running the program.

Open in playground This output was verified at build time
ስምህ ማን ነው? እንኳን ደህና መጣህ አበበ

Running this one asks you for the answer first.

Exercise

Write a function that returns whether a number is even.

Show the answer
Open in playground This output was verified at build time
እውነት
ሃሰት