The following function is supposed to take as arguments a length
expressed in feet and inches and return the total number of inches in that
many feet and inches. For example, total_inches(1, 2) is supposed to
return 14, because 1 foot and 2 inches is the same as 14 inches. Will the
following function perform correctly? If not, why not?
double total_inches(int feet, int inches)
{
inches = 12*feet + inches;
}
The provided function will perform correctly and return the total number of inches in the given feet and inches. The function takes two arguments, feet
and inches
, and converts them into a single value representing the total inches.
Let's analyze the function step by step:
inches = 12 * feet + inches;
: This line calculates the total number of inches by multiplying the number of feet by 12 (since there are 12 inches in a foot) and then adding the remaining inches.
return inches;
: The function returns the calculated total inches.
For example, if you call the function with total_inches(1, 2)
, it will calculate the total inches as 14 (1 foot * 12 inches/foot + 2 inches) and correctly return the value 14.
So, yes, the provided function will perform correctly and return the total number of inches in the given feet and inches.