Call your function inside the while loop so that it will print out a square given the user input for the length of a side.

Run the program again and verify you see results like the following:

I will print squares for you!
Enter the length of a side (-1 to quit): 5
*****
*****
*****
*****
*****
I will print squares for you!
Enter the length of a side (-1 to quit): -1
Thanks for "squaring" with me!

Submit your final program source code to Canvas as part of assignment 8.

In this exercise we trace the flow of control in a program where functions call other functions, both void and non-void functions.

Specifications

Create a text file named trace.txt.

In the trace.txt file, list the line numbers of each statement of the program shown below in the order the lines are executed. For example, the following are the first few line numbers of the program flow (assuming main() starts on line 20):

20, 21, 22, 36, 37, ...

Do not bother to list blank lines or lines containing only the closing curly brace (}) of a function definition. For more information on tracing, see section 8.2.4: Tracing Code.

Review your trace.txt file with another student to verify it is correct.

Compile and run the starter program to make sure you entered it correctly. When you run the program, the output should look like this:

Enter two integers: 1 2
After calling function: 1 2

Notice that num1 and num2 have the same values before and after calling the function swap(). Any value assigned to var1 and var2 have no effect on num1 and num2. For more information, see section: 8.3.1: Parameter Passing and Value Parameters.

Compile and run the modified program to make sure you made the changes correctly. When you run the program, the output should look like this:

Enter two integers: 1 2
After calling function: 2 1

Notice that num1 and num2 have different values before and after calling the function swap(). Any value assigned to var1 and var2 change num1 and num2 respectively. For more information, see section: 8.3.2: Using Reference Parameters.

Submit your final program source code to Canvas as part of assignment 8.

8.4.5: Functions as Black Boxes

Whenever we develop a function, it is important to design it as a black box

Black box: a technical term for a device, system or object viewed in terms of its input and output without knowledge of its internal workings.

Here is a diagram of the concept:

A black box is something that we know how to use but not how it operates

An example might be a cell phone or an iPod

These devices have controls that we know how to operate

However, we really do not know their internal workings

Designing Functions

A function has a specific job to do and should be designed like a black box

For our example, the purpose of the function is to calculate the price per square inch of a pizza

A programmer using the function needs to know what the function does and how to call it

However, a programmer should not need to know how the function does its job

This is known as, "treating the function like a black box"

Calling something a black box is a figure of speech

It is intended to convey the image of a physical device that we know how to use but not details of how it operates

If a function is well designed, then a programmer can use it as if it were a black box

A programmer should not have to look at the body of a function to know what the function does

All a programmer needs to know is what arguments are passed to the function and what value will be returned

When writing a function, we start with the function prototype

For instance, here is a possible prototype for our function:

double calcUnitPrice(int diameter, double price);

Notice the information our function prototype (header) provides

A programmer knows what arguments must be sent to the function and the type of value the function returns without needing to see the function code

However, a programmer may need more information to fully utilize the function

Function Comment Blocks

An important technique for providing information about a function is the function comment block

A function comment block is a description of:

What the function does

The purpose of each parameter

What the function returns

For example, here is a possible comment block for our function:

/**
Calculates the price per square inch of a pizza
@param diameter The diameter of the pizza in inches.
@param price The price of the pizza in dollars.
@return The price per square inch of a pizza.
*/
double calcUnitPrice(int diameter, double price);

A function prototype with a comment block should tell the programmer all the information needed to use the function

However, the function comment block should not describe how the function does its task