So far in this course, we have

Examples:

We use

$$f(x)=x+3$$

To

$$f(2) = 2+3$$

#&f(2)#& means replace #&x#& in the expression with 2.

We use the keyword

To find the value of the function when

- Define new functions in the
Definitions window - Click
Run after writing a definition - Call the function using the
Interactions window

Suppose the price of a giant box of Nerds is $10.25. How much does it cost to buy four boxes?
To find the total cost of items, we multiply price × quantity. Here is the definition of a function to do that: The The variables When we |

For example, if we type

Suppose we want to create rectangles that are always solid and red. We can define the following function:

Then we can create solid rectangles by typing

The Pythagorean Theorem shows the relationship among sides of a right triangle: $$a^2+b^2=c^2$$ We can use this theorem to find the length of the hypotenuse: $$c= \sqrt {a^2+b^2}$$

In a program, we might label the legs of the triangle

Write a function definition for each of the following calculations. Test your definitions using the interactions window.
If you need help, press the

1. The formula for area of a rectangle is length × width.

Write a define statement in the

2. The formula for distance traveled is rate × time.

This definition is just like the last one, but the names are different.

3. Write a function to find the area of a triangle, given its base and height.

The area of a triangle is `base` × `height` / 2.

4. The area of a trapezoid is #&A=\frac {(b_1+b_2)h}{2}#&.

Use these variable names:

5. Write a function to find the volume of a right rectangular pyramid, given its base length, base width, and height.

6. Write a function to find slope of a line passing through two points.

#&m= \frac {y_2-y_1} {x_2-x_2}#&

7. Write a function that finds (produces) the distance between two points #&(x_1, y_1)#& and #&(x_2, y_2)#&.

Use the distance formula, which is derived from the Pythagorean Theorem. Test this function using these points: (1,1) and 5,4).

8. Write a function that finds (produces) the perimeter of a triangle, given three points #&(x_1, y_1)#&, #&(x_2, y_2)#&, and #&(x_3, y_3)#&.

This function should use the function you wrote for exercise 7.

9. Write a function that uses Heron's Formula to find the area of a triangle define by three points. #&(x_1, y_1)#&, #&(x_2, y_2)#&, and #&(x_3, y_3)#&.

Heron's Formula: #&A=\sqrt {p(p-a)(p-b)(p-c)}#&, where #&p#& is half the perimeter of the triangle, and #&a,b,c#& are the side lengths.

It is possible to write this function using the function defined in exercise 8. However, this new function will be very long an tedious. There are better ways of solving this problem, which you will learn in a later lesson.

The following function definitions contain syntax errors. For each function, do the following:

- Use Dr. Racket's definitions window to find and correct the syntax error.
- Test the function using the interactions window.
- Write the corrected function definition and test results in your workbook.

1.

;; This function should create a red square

2.

;; This function finds the perimeter of a square

3.

;; This function finds the perimeter of an equilateral triangle

4.

;; This function finds the area of a triangle

5.

;; THis function converts feet to inches

6.

;; This function calculates density

7.

;; This function finds the product of three numbers

8.

;; This function finds the sum of three numbers

9.

;; This function finds the average of three numbers

Computer Programming Unit 3.2 - Defining Functions