Functions with Local Variables:
The functions we have seen faraway have been very easy. Though, in many situations the computations in a function are more complex, and may need the use of extra variables within the function; these are known as local variables.
For illustration, a closed cylinder is being constructed of a material which costs a dollar amount per square foot. We will write a function which will compute and return the cost of the material, rounded up to the nearby square foot, for a cylinder with a certain radius and a certain height. The total surface area for the closed cylinder is
SA = 2πrh + 2πr^{2}
For illustration, for a cylinder with radius of 32 inches and height 73 inches, and the cost per square foot of material is $4.50, the computation would be given by the algorithm shown below:
(A) Compute the surface area SA = 2*π * 32 * 73 + 2 * π * 32 * 32 inches squared
(B) Compute the SA in square feet = SA/144
(C) Compute the total cost = SA in square feet * cost per square foot
The function involves local variables to store the intermediate results.