Giter Site home page Giter Site logo

function-arguments-readme-chi01-dtsc-ft-042020's Introduction

Function Arguments

Introduction

Function arguments are a powerful tool in programming. As we'll see, arguments make our functions more flexible and reusable by explicitly allowing different inputs to be used in a function and changing the output of the function based on these inputs.

When used correctly, function arguments bring clarity to what inputs a function needs to operate, as well as how a function uses these inputs.

Learning Objectives

  • Understand how function arguments make functions more flexible and predictable
  • Understand how to define and execute a function with arguments

Predictability with arguments

In the previous lesson, we saw that functions were a powerful tool. They allow us to repeat operations and apply these operations to different data. For example, take a look at a function called meet_traveller.

def meet_traveller(): 
    welcome_message = "Hi " + traveller.title() + ", I'm so glad we'll be going on the trip together!"
    return welcome_message # return statement

The meet_traveller function is designed to generate nice greetings to each new employee. Do we need anything else to run the function? How do we know which new employee the function will greet? Let's run the function and see what happens.

meet_traveller()
---------------------------------------------------------------------------

NameError                                 Traceback (most recent call last)

<ipython-input-47-808d0f7c17d9> in <module>()
----> 1 meet_traveller()


<ipython-input-46-70bd219b8158> in meet_traveller()
      1 def meet_traveller():
----> 2     welcome_message = "Hi " + traveller.title() + ", I'm so glad we'll be going on the trip together!"
      3     return welcome_messages # return statement


NameError: name 'traveller' is not defined

The function requires the variable traveller, but it's hard to tell that before running the function. When code requires something to work, we call that something a dependency. Below, our function meet depends on the variable traveller being provided, or it will not work. Ideally, our dependencies are more explicit than in our meet_traveller function. Let's adapt this function to make its dependencies more explicit.

def meet(traveller): 
    welcome_message = "Hi " + traveller.title() + ", I'm so glad we'll be going on the trip together!"
    return welcome_message

Ok, in the code above we changed the first line of the function, the function signature, to the following:

def meet(traveller):

This tells us, and Python, to not even run the code unless the proper data to the function is provided. Let's see it.

meet()
----------------------------------------------------------------------

TypeError                            Traceback (most recent call last)

<ipython-input-2-c60a3be83cb4> in <module>()
----> 1 meet()


TypeError: meet() missing 1 required positional argument: 'traveller'

Do you see that error message at the bottom there? It's pretty explicit about saying that this argument requires a positional argument traveller.

So, by using an argument, the function signature tells us how to run this function. We refer to the function by it's name and then pass through a string representing the traveller.

meet('sally')
"Hi Sally, I'm so glad we'll be going on the trip together!"

Flexibility

Let's take another look at the meet function. Notice, that the argument operates like a variable in that we can easily alter the data that traveller points to. When we pass through the string, 'Sally', the function replaces traveller with the string 'Sally'.

def meet(traveller): 
                              # "sally"
    welcome_message = "Hi " + traveller.title() + ", I'm so glad we'll be going on the trip together!"
    return welcome_message

And we can easily change what traveller points to just by passing through a different string.

meet('fred')
"Hi Fred, I'm so glad we'll be going on the trip together!"

But notice that the traveller argument is only accessible just inside of the function.

traveller
----------------------------------------------------------------------

NameError                            Traceback (most recent call last)

<ipython-input-6-12a71905bbfe> in <module>()
----> 1 traveller


NameError: name 'traveller' is not defined

So by using arguments, we can easily see what a function requires to work, change the output by passing through different data to the function, and ensure that we only have to worry about what our argument is while inside that function.

Summary

In this lesson, we saw some of the benefits of using arguments: they make our functions more flexible and predictable. Our functions are more flexible as the functions vary based on the argument provided to the function. Arguments make our functions predictable by making functions more explicit about their dependencies. They also allow us to change the value of an argument which only affects the functions internal values and more directly shows us how the output of our function will vary based on different inputs.

function-arguments-readme-chi01-dtsc-ft-042020's People

Contributors

jeffkatzy avatar cutterbuck avatar tkoar avatar

Watchers

James Cloos avatar  avatar Mohawk Greene avatar Victoria Thevenot avatar Bernard Mordan avatar Otha avatar raza jafri avatar  avatar Joe Cardarelli avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar  avatar Matt avatar Antoin avatar  avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar Ahmed avatar Nicole Kroese  avatar Kaeland Chatman avatar Lisa Jiang avatar Vicki Aubin avatar Maxwell Benton 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.