user3199576
user3199576

Reputation:

return function in python

I want to write script to calculate the volume. The first function calculates area of the base. The second takes this value and multiply it with the height. Then I'd like to write the value of the volume. Where is the mistake?

def base_area(a, b):
    a = 2
    b = 3
    s = a * b
    return s

def volume(s):
    h = 7
    V = h * s
    print (V)

Upvotes: 0

Views: 71

Answers (1)

Christian Tapia
Christian Tapia

Reputation: 34146

It doesn't make sense to pass a, b as parameters to base_area() function, because inside, you are assigning constant values to a and b. That method should look like:

def base_area(a, b):
    s = a * b
    return s

so you use the values passed. This function can be written in a more concise way:

def base_area(a, b):
    return a * b

Then the volume() method should receive 3 parameters, a, b and h(height):

def volume(a, b, h):
    return base_area(a, b) * h

Here, you are calling base_area() passing a and b. From this call, you get the area, so you multiply it by h and return it.

Test:

print volume(2, 3, 7)
>>> 42

Upvotes: 2

Related Questions