A Computer Science Tapestry- pages 1-21
Review from Friday
Functions With Parameters
In the last exercise we used functions of the form
void functionname(void)
The "voids" are placeholders for variables that could either be returned from the function (void out front) or passed to the function (void inside the parentheses). Passing variables to the function can be very useful. In the last exercise each of the two functions had to request user input for the radius. It would have been useful to get the radius once and then use the same value to compute both the area and the perimeter. This could be done by getting the value of radius in the main function and then passing it to the perimeter and area functions.
An example of passing a parameter in a function is given below for a function which squares a number.
This function only uses the number once, so the advantage of using a function that takes a parameter is not particular large. However, this does illustrate this use. Notice that the name of the variable that square is passed in main does not need to match the name in the square function. This is because the number itself is passed, not the variable.
Take this same idea and apply it to the radius and perimeter program so that you only enter the radius once and pass this value to the both functions.