The Fortran function 'cone_volume' calculates the volume of a right circular cone provided both radius and height are non-negative, using the formula V = (1/3)πr^2h.
To calculate the volume of a right circular cone in Fortran, you can use the following internal function:
function cone_volume(radius, height)
implicit none
double precision, intent(in) :: radius, height
double precision :: cone_volume
if (radius .ge. 0.0d0 .and. height .ge. 0.0d0) then
cone_volume = (1.0d0/3.0d0) * pi * radius**2 * height
else
cone_volume = 0.0d0
end if
end function cone_volume
This function ensures that the parameters are non-negative and calculates the volume using the formula V = 1/3 * π * r2 * h, where r is the base radius and h is the height of the cone. If either the radius or height is negative, the function returns 0.0 to indicate invalid inputs.
The function provided above can be incorporated into a larger Fortran program to accurately calculate cone volumes, reinforcing the importance of proper parameter validation in software development.