Pre-Lab - Lab (4)



Before coming to the lab, you should answer the following questions in writing.

1. Write a C++ program that asks users for two entries of type double, x and y, and then displays their product.  Your code should have:
    1) a function called get_x that asks the users for the value of x, then returns x to the main function.
    2) a function called get_y that asks the users for the value of y, then returns y to the main function.
    3) a function called mult_xy that takes x and y as its arguments and returns their product to the main function.

This program must use all three functions.

2. We mentioned that a function can only return one value using the return statement.  Is there any way that a function returns more than one value?  In the above program, could you read both x and y in the same function, and then return both of them to the main?
 
 
 
 
 

3. Can a function call another function? Maybe this helps you answer this question.
In the following program, is main a function?
Does it call another function?

#include<iostream>
using namespace std;

float divide_xy(int x, int y);

int main(void)
{
      int x,y;
      float qt;

      cout << "Enter 2 values for x and y separated by space, then press <Enter> :";
      cin >> x >> y;
      cout << endl;
      qt = divide_xy(x,y);

      cout << x << "/" << y << " = " << qt << endl;

      return 0;
}

float divide_xy(int x, int y)
{
      return static_cast<float>(x)/y;
}

4. Explain how you may have use for a function that does not return any value?
 

5. Explain how you may have use for a function that does not have any argument?