Introduction To Functions Lab


As we know, we can use functions to name sequences of our code, thus making our code more expressive. We can also use functions to allow us to reuse our code. In this lab we will practice using functions for these purposes.


  • Practice declaring and returning values from functions
  • Practice accessing variables that are outside of a function's scope, from inside of a function

Writing our first functions

Imagine we are working on our list of travel destinations -- which is really turning out to be a full time job. We have our list of travel_destinations which we assign below. Write a function called number_of_destinations that returns the number of destinations we have on our list.

travel_destinations = ['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia']
def number_of_destinations():
number_of_destinations() # 6

Now write another function called next_up that returns our first destination (the destination with the lowest index), in the travel_destinations list.

def next_up():
travel_destinations = ['argentina', 'mexico', 'italy']
next_up() # 'argentina'
travel_destinations = ['finland', 'canada', 'croatia']
next_up() # 'finland'

Ok, now write a function called favorite_destination that returns the string 'madagascar'.

travel_destinations = ['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia']
def favorite_destination():
favorite_destination() # 'madagascar'

Again, let's declare an array called travel_destinations. Change the function favorite_destination so that it continues to return the string 'madagascar', but also adds the string 'madagascar' to the end of the list of destinations.

travel_destinations = ['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia']
travel_destinations[-1] # 'madagascar'

Now let's write another function which iterates through the list of destinations and capitalizes the first letter of each word. It should return a list of capitalized destinations.

travel_destinations = ['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia']
def capitalize_countries():
capitalize_countries() # ['Argentina', 'Mexico', 'Italy', 'Finland', 'Canada', 'Croatia']

Great! Now if someone adds a country that is lowercased to our list of destinations, we can simply call our function again to capitalize each of the destinations in the list.

travel_destinations = ['argentina', 'mexico', 'italy', 'finland', 'canada', 'croatia']
capitalize_countries() # ['Argentina', 'Mexico', 'Italy', 'Finland', 'Canada', 'Croatia']
capitalize_countries() # ['Argentina', 'Mexico', 'Italy', 'Finland', 'Canada', 'Croatia', 'Japan']


Great job! In this lab we were able to get practice both writing and returning values from functions. We also practiced accessing variables not local to the function but in the global scope.

Unlock your future in tech
Learn to code.

Learn about Flatiron School's Mission

With a new take on education that falls somewhere between self-taught prodigy and four-year computer science degree, the Flatiron School promises to turn students with little programming experience into developers.

In the six months since the Manhattan coding school was acquired by WeWork, it has spawned locations in Washington, D.C., Brooklyn, and London. Now, WeWork is opening a fourth Flatiron School location, this time in Houston.

Adam Enbar, Flatiron School's cofounder, believes now is the time to grow. "How the world is changing has impacted working and learning in very similar ways. We think education fundamentally is about one thing: enabling people to pursue a better life."

Learn. Love. Code.
Students come to Flatiron School to change their lives. Join our driven community of career-changers and master the skills you need to become a software engineer or a data scientist.
Find Us