In this chapter, we'll explore the story of the Tower of Babel and learn about functions and return statements in Python programming. The Tower of Babel story will teach us about the importance of communication and cooperation, and how pride can lead to division. We'll see how these lessons can be related to functions in programming, which help us break down complex tasks into simpler, reusable components.

Read the Bible

Genesis 11:1-9

Reflection

  1. What can we learn from the story of the Tower of Babel about pride and the importance of communication?
  2. How can breaking down complex tasks into smaller, manageable components help us in our everyday lives, just as in programming?

Learn about programming

Functions

Functions are like small programs within a program that perform a specific task. They help us break down complex tasks into smaller, manageable components.

Functions are a way to group a block of code so that it can be used multiple times. Functions make our code more readable and easier to maintain. The basic structure of a function is as follows:

def function_name(arguments):
    # Code to be executed
    return result

Let's create a function related to the Tower of Babel story. In the story, people tried to build a tower to reach the heavens, and their languages were confused. We'll create a function that constructs a "tower" of text using words from different languages.

# define a function to build tower
def build_tower(language, height):
    if language == "English":
        word = "Tower"
    elif language == "Spanish":
        word = "Torre"
    elif language == "French":
        word = "Tour"
    else:
        word = "Unknown"

    tower = ""
    for i in range(height):
        tower += word + "\\n"
    return tower

# define inputs to the build_tower function
language = "English"
height = 4

# check outputs of the build_tower function
tower = build_tower(language, height)
print(tower)

build_tower.gif

In this example, the build_tower function takes two arguments: language and height. Depending on the chosen language, the function will use a different word for "tower". Then, the function will construct a "tower" of text with the chosen word repeated height times, and return it as the output of the function.

Exercise

Create a function called check_height that takes a height in meters and returns whether the tower is too short, too tall, or just right based on the following criteria: