Reference no: EM133105188
Assignment: Parameter passing and local variables
Now that you have played around with some simple subroutines it is time to gain a more complete understanding of this important programming construct. In this assignment you will write a subroutine that takes several input parameters and returns a computed value. If you have not already done so, study the remainder of paragraph 3.3.2, which discusses the techniques and conventions surrounding subroutine parameter passing and return values.
Exercises:
Question 1: The following partial specification of the pow subroutine is given:
/**
* The pow subrout ine calculates powers of natural bases
* and exponents .
*
* Arguments :
*
* base = the exponential base
* exp = the exponent
*
* Return value : ' base ' raised to the power of ' exp ' .
*/
int pow( int base , int exp ) {
int total = 1 ;
// . . .
return total ;
}
Complete the specification of the pow subroutine. You should only use looping constructs and simple arithmetic operations to compute the total.
Question 2: You may have your specification checked by one of the lab course assistants. The lab course assistants can prevent you from implementing the wrong algorithm. If you are confident your specification is correct, you don't have to ask an assistant to check and you may immediately proceed with the next step.
Question 3: Write a program called "power" which contains an implementation of your pow subroutine. The main routine should ask the user for a positive base and exponent. The program should then calculate the resulting power using the pow subroutine and print the return value.