TRY IT: Working with Functions and Function Calls Step 1: Type the following pro
ID: 3853930 • Letter: T
Question
TRY IT: Working with Functions and Function Calls
Step 1: Type the following program and add it to the Lab9 project. See the function call example discussed in the class (maximum) for the proper syntax.
12 /***** main *****/
13 int main()
14 { int value = 2;
15
16 cout << "Hello from main. ";
17 printMessage();
18
19 cout << " Value returned by tripleIt is "
20 << tripleIt(value) << endl;
21 cout << "In main value now is "
22 << value << endl << endl;
23
24 value = tripleIt(value);
25 cout << "In main value now is "
26 << value << endl;
27
28 value = tripleIt(value);
29 cout << "In main value now is "
30 << value << endl << endl;
31
32 cout << "Goodbye from main. ";
33 return 0;
34 }
35
36 /***** printMessage *****/
37 void printMessage()
38 {
39 cout << "Hello from PrintMessage. ";
40 }
41
42 /***** tripleIt *****/
43 int tripleIt(int someNum)
44 {
45 return someNum * someNum * someNum;
46 }
Expected Output
Observed Output
Step 2: Read the source code, paying special attention to the flow of control from main to the functions it calls and then back to main again. Notice what main passes to each function and what, if anything, the function return. Once you have done this, complete the “Expected Output” box in the table above, writing down what the program will display in the order it will be displayed.
Step 3: Now compile and run the above program, and look at the output it creates. If the actual output matches what you wrote down, just place a checkmark in the “Observed Output” box. If it is not the same, write down the actual output.
12 /***** main *****/
13 int main()
14 { int value = 2;
15
16 cout << "Hello from main. ";
17 printMessage();
18
19 cout << " Value returned by tripleIt is "
20 << tripleIt(value) << endl;
21 cout << "In main value now is "
22 << value << endl << endl;
23
24 value = tripleIt(value);
25 cout << "In main value now is "
26 << value << endl;
27
28 value = tripleIt(value);
29 cout << "In main value now is "
30 << value << endl << endl;
31
32 cout << "Goodbye from main. ";
33 return 0;
34 }
35
36 /***** printMessage *****/
37 void printMessage()
38 {
39 cout << "Hello from PrintMessage. ";
40 }
41
42 /***** tripleIt *****/
43 int tripleIt(int someNum)
44 {
45 return someNum * someNum * someNum;
46 }
Expected Output
Observed Output
Explanation / Answer
EXPECTED OUTPUT :
Hello from main.
Hello from PrintMessage.
Value returned by tripleIt is 8
In main value now is 2
In main value now is 8
In main value now is 512
Goodbye from main.
OBSERVED OUTPUT:(both expected and observed are same)
Hello from main.
Hello from PrintMessage.
Value returned by tripleIt is 8
In main value now is 2
In main value now is 8
In main value now is 512
Goodbye from main.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.