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 — ተግባር.
ሰላም አበበ
ሰላም አልማዝ
ተግባር comes first, then the name, then the inputs it accepts in
parentheses.
Returning a value
A function can hand a value back with ተመላሽ.
7
30
ተመላሽ stops the function immediately.
12
9
Why they are worth it
A function gives you three things:
- No repetition — write it once, call it many times
- A name —
ትልቁ(ሀ፣ ለ)says more than three lines of comparison - 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.
120
ፋክቶሪያል(5) becomes 5 * ፋክቶሪያል(4), then 5 * 4 * ፋክቶሪያል(3), down to
- 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.
ስምህ ማን ነው? እንኳን ደህና መጣህ አበበ
Running this one asks you for the answer first.
Exercise
Write a function that returns whether a number is even.
Show the answer
እውነት
ሃሰት