Skip to main content

11.6) Exercise 31 - Functions


Here’s a function with an argument that returns a value. You usually need a bit more documentation in these cases. Type it in and check that you understand how it works.

def vol_sphere(radius):
    """Calculate volume of a sphere
    Argument:
        radius: radius of the sphere
    Returns:
        Volume of the sphere as a float
    """
    pi=3.14	# local variable
    return 4./3.*pi*(radius**3)
# now use the function
r=2.5 # cm
V=vol_sphere(r) # cm3
print 'Volume=',V