ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ
Defining functions
You've used functions since your first program: print, then input and len. Now
you'll make your own. A function gives a piece of code a name, so you can run it
whenever you need it, and it can hand you back a result. By the end you'll know the
difference that trips up almost everyone: printing a value versus returning it.
Making a function
This program makes a function. Predict what it prints.
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
def greet():
print("Selam")Python prints
Nothing (no output)
def only makes the function and gives it a name. The line inside it runs when you call the function, and this program never does.
def is how you
a function: the word def, a name, parentheses, a colon, then the indented lines that
belong to it (a block, like an if's). Defining a function doesn't run those lines. It
makes a function and gives it the name greet, ready for later.
To run them, you the function: its name, then parentheses. Each call runs the whole block again.
def greet():
print("Selam")
greet()
greet()In the editor, press Escape then Tab to move on.
The parentheses are what make it a call. Predict this one:
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
def greet():
print("Selam")
greetPython prints
Nothing (no output)
Without parentheses, greet is just the function's name. Python looks it up and does nothing with it, so nothing runs and nothing prints.
A function is a value, like a number or a list, and greet is a name that refers to
it. print(greet) shows a description of the function object (it starts with
<function greet at), not "Selam". If a function you wrote seems to do nothing, check
for the missing () first.
Giving a function information
A function gets more useful when you can hand it something to work on. Put a name
inside the parentheses of the def:
def greet(name):
print(f"Selam, {name}!")
greet("Hana")
greet("Abel")In the editor, press Escape then Tab to move on.
name is a
:
a name the function uses for whatever it's given. The value in the call, "Hana", is
an
.
In each call, name refers to that call's argument. Now predict what happens when the
value comes from a name of your own:
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
def greet(name):
print(f"Selam, {name}!")
person = "Hana"
greet(person)Python prints
Selam, Hana!
The argument is the value person refers to, "Hana". Inside the call, the parameter name refers to that same value. The two names don't have to match: Python passes the value, not the name.
Handing a value back
Printing shows a value to a person. Often you want the value itself, to give it a name
or calculate with it. return hands a value back to the code that called the function.
Ethiopia's standard VAT is 15%:
def vat(price):
return price * 1.15
total = vat(250)
print(total)
print(vat(1000))In the editor, press Escape then Tab to move on.
The value a call hands back is its
.
vat(250) becomes 287.5, just as if you'd typed 287.5 there.
Each call gets its own
,
a set of names that belong to that call only. The diagram below draws frames as boxes
of names, with arrows to the values. Line 4 shows up twice: first when the call starts
(a frame for vat appears, and price refers to 250), then again when the call is over
and total gets the returned value.
One call to vat
Press Next to run one line at a time. Each arrow shows the object a name refers to. Each call gets its own frame of names, below the frame that called it.Each row is one step: the line that just ran, and the object each name refers to after it, frame by frame, oldest first.
Frames, oldest first: global.
Step 1 of 5
Step 1 of 5. Line 1: def vat(price):. Nothing in memory changed.
| Step | Line | Names and objects |
|---|---|---|
| 1 | 1 |
|
| 2 | 4 |
|
| 3 | 2 |
|
| 4 | 4 |
|
| 5 | 5 |
|
When the call ends, its frame and price are gone. Only the returned value is left,
and total refers to it. The top frame, global, has the names made outside any
function. (vat is one of them too; the diagram leaves it out until lesson 5.5.)
Keep one rule in mind: print is for people; return is for the program. print
shows a value on the screen and hands nothing back. return hands a value back to the
code and shows nothing. Here's why it matters. Predict:
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
def greet_print(name):
print(f"Selam, {name}!")
x = greet_print("Hana")
print(x)Python prints
Selam, Hana! None
Calling greet_print prints the greeting. But it has no return, so it hands back None, and x refers to None. The second print shows it.
A function without return still hands something back:
,
Python's value for "nothing". Printing inside a function doesn't make that value its
return value. You only notice when you try to use the result:
ጥያቄ
What does a function return if it has no return?
return ends the call
Predict what this prints.
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
def vat(price):
return price * 1.15
print("VAT added")
print(vat(250))Python prints
287.5
return hands the value back and ends the call on the spot. The print after it never runs.
Lines after a return in the same block never run. Put anything the function needs to
do before its return.
A return inside an if ends the call only when that branch runs. Otherwise Python
carries on to the lines below it, so a function can have more than one return, and
the first one reached wins:
def ride_type(km):
if km > 10:
return "long"
return "short"
print(ride_type(12))
print(ride_type(3))In the editor, press Escape then Tab to move on.
For 12 km, the first return ends the call. For 3 km, the if is skipped and the
second return runs. You'll use this shape in almost every function from now on.
መልመጃ
Santim to birr
100 santim make 1 birr. Write santim_to_birr(santim) so it returns the amount in
birr, then use it in the f-string. Don't print inside the function.
2550 santim is 25.5 birrYour code
def santim_to_birr(santim):
# Return the amount in birr
pass
price = 2550
print(f"{price} santim is {santim_to_birr(price)} birr")In the editor, press Escape then Tab to move on.
መፍትሄውን አሳይHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
def santim_to_birr(santim):
return santim / 100
price = 2550
print(f"{price} santim is {santim_to_birr(price)} birr")ዋና ዋና ነጥቦች
defdefines a function; nothing inside it runs until you call it with().- Parameters are the names in the
def; arguments are the values in the call. Only the value is passed, sogreet(person)works with a parameter calledname. - Print is for people; return is for the program:
returnhands a value back and ends the call, even inside anif. - A function with no
returnreturnsNone, so using its result fails. - Each call gets its own frame of names, gone when the call ends.