GithubHelp home page GithubHelp logo

introduction-to-functions-lab-nyc-clarke-ds-111819's Introduction

Functions lab

Introduction

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.

Objectives

  • 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():
    pass
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():
    pass
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():
    pass
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']
favorite_destination()
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():
    pass
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']
travel_destinations.append('japan')
capitalize_countries() # ['Argentina', 'Mexico', 'Italy', 'Finland', 'Canada', 'Croatia', 'Japan']

Summary

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.

introduction-to-functions-lab-nyc-clarke-ds-111819's People

Contributors

jeffkatzy avatar tkoar avatar cutterbuck avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.