Search code examples
pythonvariablescallfunction

Replacing a Function with a Variable?


I feel like this question is going to be silly, because I'm not entirely sure how to phrase it. I think it's best just to not over explain my question.

Okay, so on Code Academy they gave this assignment:

Below your existing code, define a function called trip_cost that takes two arguments, city and days.

Like the example above, have your function return the sum of calling the rental_car_cost(days), hotel_cost(days), and plane_ride_cost(city) functions.

Why/ How does the following work (I get it, but I don't get it.):

It is completely valid to call the hotel_cost(nights) function with the variable days. Just like the example above where we call double(n) with the variable a, we pass the value of days to the new function in the argument nights.

I did the code right and passed:

def hotel_cost(nights):
    return 140 * nights
def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475
def rental_car_cost(days):
    cost = 40 * days
    if days >= 7:
        cost -= 50
    elif days >= 3:
        cost -= 20 
    return cost    
def trip_cost(city, days):
    return plane_ride_cost(city) + hotel_cost(days) + rental_car_cost(days)

At first I put hotel_cost(nights), but then I changed it to hotel_cost(days) and passed. Now it strongly confused me. How & Why does the function grab from variables from the def? I guess I'm asking how does that work?

I'm trying to understand how to phrase the question. Can someone Break this down? Haha I don't even know what to title this because the concept is a bit confusing. I mean I get it, but I don't understand it (If that makes sense.) An does this only work when defining a function?

Also, why is it called "Calling a function", when in this case it's actually replacing a function?


Solution

  • When you define a function e.g.: def hotel_cost(nights) the variable nights is just a name of whatever you pass into the function. It doesn't have to match the name of whatever you send in.

    So when you're saying def trip_cost(city, days): Python is saying: "Ok I'm expecting two variables in this function, one named city and one named days.

    So when you try to call

    def trip_cost(city, days):
        return plane_ride_cost(city) + hotel_cost(nights) + rental_car_cost(days)
    

    Python says, "well I only know about city and days. There is no such thing as a nights variable to me and you'll get an error.

    This is the case even if you call something like cost = trip_cost(city, nights). While it is nights to the caller. As far as the function knows it only sees that second thing as days once it executes inside the function.

    In a more technical definition, this is called scope. The scope of a function is what it can "see" outside of itself and what it cannot. In this case the only thing trip_cost can see is the two things given to it, the thing in position 1 which you defined as city and the thing in position 2 which you defined as days.