In: Computer Science
The expression below (approximately) calculates the volume of a cylinder. Extract the expression into a function named calculate_volume, and then replace the existing calculations with function calls.
Do in Python
The expression written in the given print() calls are extracted into the function calculate_volume() and the existing calculations in the print() are replaced by the
function calls.
The complete code is given as follows:
Screenshot of the code:
Sample Output:
Code to copy:
#Define the function calculate_volume() having two parameters
#radius and height.
def calculate_volume(radius, height):
#Initialize a variable PI with the value 3.14159.
PI = 3.14159
#Calculate the volume of cylinder by using the
#formula PI * (radius^2) * height by placing the
#value of PI, radius, and height value in this
#formula.
volumeOfCylinder = PI * (radius ** 2) * height
#Return the volume of cylinder calculated above.
return volumeOfCylinder
#Call the function calculate_volume() by passing different
#arguments.
print(calculate_volume(1, 2))
print(calculate_volume(5, 1))
print(calculate_volume(2, 3.1))