Question 1 Assume a program contains a void function named c
Question 1
Assume a program contains a void function named calcNewPrice(). The function receives two double variables named oldPrice and newPrice. The function multiplies the contents of the oldPrice variable by 1.1, then stores the result in the newPrice variable. Which of the following can be used to call the calcNewPrice() function?
A. calcNewPrice(double oldPrice, double newPrice);
B.calcNewPrice(&oldPrice, newPrice);
C. calcNewPrice(oldPrice, &newPrice);
D. calcNewPrice(oldPrice, newPrice);
Question 2
Assume a program contains a void function named calcNewPrice(). The function receives two double variables named oldPrice and newPrice. The function multiplies the contents of the oldPrice variable by 1.1, then stores the result in the newPrice variable. Which of the following is the best function prototype for this function?
A. void calcNewPrice(double oldPrice, double newPrice);
B. void calcNewPrice(double &oldPrice, double newPrice);
C. void calcNewPrice (double oldPrice, double &newPrice);
D. void calcNewPrice(double &oldPrice, double &newPrice);
Solution
Function calcNewPrice():
Input Parameters: Two double variables named oldPrice and newPrice
Purpose: Multiplies the contents of the oldPrice variable by 1.1, then stores the result in the newPrice variable
Question 1: Function call
(A) calcNewPrice(double oldPrice, double newPrice)
This function call is invalid. Declaring variable\'s with in the function call is not allowed. Hence this is not correct answer.
(B) calcNewPrice(&oldPrice, newPrice);
This function call is invalid. First parameter is address of variable oldPrice but the actual function definition accepts normal double variable but not pointer variable.
Hence this is not the correct answer.
(C) calcNewPrice(oldPrice, &newPrice);
This function call is invalid. Second parameter is address of variable newPrice but the actual function definition accepts normal double variable but not pointer variable.
Hence this is not the correct answer.
(D) calcNewPrice(oldPrice, newPrice);
This function call passes the contents of variables oldPrice and newPrice. Actual function definition also accepts two double variables.
Hence this is the correct answer.
Hence Correct option is (D)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Question 2: Function Prototype
(A) void calcNewPrice(double oldPrice, double newPrice);
This function header creates a copy of arguments which doesn\'t have any reflection on values of variables in the caller. Hence this is not the correct answer.
(B) void calcNewPrice(double &oldPrice, double newPrice);
This function header oldPrice is declared to be of type pass by reference which reflects its value in the caller. But function calcNewPrice should reflect the value of variable newPrice but not oldPrice.
Hence this is not the correct answer.
(C) void calcNewPrice(double oldPrice, double &newPrice);
This function header oldPrice is declared to be of type pass by value which doesn\'t reflect its value in the caller and newPrice is declared to be of type pass by reference which reflect its value in the caller. Actual function calcNewPrice should reflect the value of variable newPrice but not oldPrice.
Hence this is the correct answer.
(D) void calcNewPrice(double &oldPrice, double &newPrice);
This function header variable oldPrice and newPrice both are declared to be of type pass by reference which reflects its value in the caller. But actual function calcNewPrice should reflect the value of variable newPrice but not oldPrice.
Hence this is not the correct answer.
Hence Correct option is (C)

