انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Python: home
0. Lab tour1
1. The start5
2. Variables and input5
3. Conditions5
4. Loops5
5. Lists4
6. Functions4
7. Drawing with the turtle3
8. The project: the guess-the-number game3
9. Strings in depth1
10. Dictionaries (dict)3
11. Randomness and errors3
12. Classes and objects (class)2
13. Pro drawing and charts3
14. The final project: the quiz game2

return: the function gives back a result

Section 6: FunctionsLesson 6.2 · ⏱ 7 min
def area(w, h):
    return w * h

room = area(4, 5)
print("Room area:", room)
print("Big board:", area(10, 2))
🖥️ The output in Robo Python:
Room area: 20
Big board: 20
🐍 Open the code in Robo Python ↗
The example in Robo Python: the code on the right and what appeared on the left
The example in Robo Python: the code on the right and what appeared on the left

📌 Another example: a function that says yes or no

return True or False, and we use it inside if.

🐍 A function that says yes or no
def is_even(n):
    return n % 2 == 0

for n in [3, 8]:
    if is_even(n):
        print(n, "even")
    else:
        print(n, "odd")
🖥️ The output in Robo Python:
3 odd
8 even
🐍 Open the code in Robo Python ↗
✍️ Practise
Try the lesson's examples here: copy the code, change it, and click ▶ Run.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

A square function that returns the square of the number.

👀 Show the solution
🐍 Solution
def square(n):
    return n * n

print(square(9))
🖥️ The output in Robo Python:
81
🐍 Open the code in Robo Python ↗

Result: 81

Medium

An area function that returns a rectangle's area.

👀 Show the solution
🐍 Solution
def area(w, h):
    return w * h

print(area(4, 6) + area(2, 3))
🖥️ The output in Robo Python:
30
🐍 Open the code in Robo Python ↗

Result: 24 + 6 = 30

Hard

A biggest function that returns the biggest number in a list (without max).

👀 Show the solution
🐍 Solution
def biggest(nums):
    best = nums[0]
    for n in nums:
        if n > best:
            best = n
    return best

print(biggest([4, 17, 9, 12]))
🖥️ The output in Robo Python:
17
🐍 Open the code in Robo Python ↗

Result: 17

Finished the lesson? Mark it so your progress counts.