Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda for Unit 3: Functions

Similar presentations


Presentation on theme: "Agenda for Unit 3: Functions"— Presentation transcript:

1 Agenda for Unit 3: Functions
Objectives 3. Define functions to perform game programming tasks. 3.1: Define the purpose of a void function. 3.2: Identify the method used to divide a program into manageable functions. 3.3: Explain how to call a function in a program. 3.4: Summarize the purpose of arguments in programming.

2 Hour 1 Review Quiz 2.1 Review Homework 2.1 Quick Hits
Review week 2 lab (Duck Hunt)

3 THAT’S IT! STAY AWAKE AND PAY ATTENTION!
Hour 2 Chapter 4: void Functions THAT’S IT! STAY AWAKE AND PAY ATTENTION!

4 Hour 3 Chapter 4: void Functions (cont.) Lab 3.1 Modular Snowman

5 Hour 4 Lab 3.1 Modular Snowman (cont.) Quiz 3.1 Quiz 3.1 answers
Homework 3.1 assignment

6 Ready?

7 Hour 1: Quiz 2.1 Stats Number of Students that scored 100% = 7 Number of Students that scored 90% = 1 Number of Students that scored 80% = 1 Number of Students that scored 40% = 4 Number of Students that scored 20% = 4 Number of Students that scored 15% = 1 Number of Students that scored 0% = 2 Class Average = 56.25%

8 Hour 1: Answers to Quiz 2.1: pg 136
Assume the variables result, w, x, y, and z are all integers, and that w = 5, x = 4, y = 8, and z = 2. What value will be stored in result after each of the following statements execute? a. result = x + y; result = 4 + 8 result = 12 b. result = z * 2; result = 2 * 2 result = 4

9 Hour 1: Answers to Quiz 2.1: pg 136 (cont.)
c. result = y/x; result = 8/4 result = 2 d. result = y - z; result = result = 6

10 Hour 1: Answers to Quiz 2.1: pg 136 (cont.)
2. Write a statement that assigns the sum of 10 and 14 to the variable total. total = ; 3. Write a statement that divides the variable length by 2 and assigns the result to the variable midpoint. midpoint = length /2; 4. Write a statement that gets the width of the program’s window and stores that value in a variable named width. width = dbScreenWidth( ); 5. Write a statement that gets the height of the program’s window and stores that value in a variable named height. height = dbScreenHeight ( );

11 Hour 1: Answers to Homework 2.1:
Turn to page 133 and read aloud: 1. C 2. C 3. A 4. D 5. B 6. D 7. B

12 Hour 1: Answers to Homework 2.1: (cont.)
Turn to page 134 and read aloud: 8. A 9. B 10. C 11. A 12. D 13. C 14. B 15. A

13 Hour 1: Quick Hits Make sure you empty your folder each week.
Windows 7 and Visual Basic C++ Express Edition do not play well together. Procedure to reuse your programs code Start new Project! Cut and Paste code into new .cpp file

14 Hour 1: Review week 2 lab (Duck Hunt)
CODE NOT AVAILABLE: STILL HAVE STUDENTS WHO HAVE NOT TURNED IN

15 BREAK!

16 Hour 2: Chapter 4: void Functions
You should have read this over the break so the lecture should be a REVIEW! Monitors off!

17 BREAK!

18 Hour 3: Chapter 4: void Functions
You should have read this over the break so the lecture should be a REVIEW! Monitors off!

19 Hour 3: Lab 3.1: Modular Snowman
(Programming Exercise #1 on pages of Starting Out with Games and Graphics in C++) What is the purpose? This lab is designed to give you the opportunity to write a modular program. What are the steps? Task 1: Write a program that draws a snowman using only one function. Procedure 1. Draw a snowman with two circular body sections, a head, two line segment arms, facial features (eyes, nose, mouth), and a hat. Refer to the diagram on page 177 of the textbook for an example. This initial version should consist of only one function. 2. Run the program and verify that it works. 3. Save this version of the project to a separate folder so that you can return to it later. Continue Task 2 using the original project files.

20 Hour 3: Lab 3.1: Modular Snowman (cont.)
Task 2: Rewrite the program so that it is modular. Procedure 1. Rewrite (or “refactor”) the program so that the code is divided into the following functions: drawBase drawMidSection drawArms drawHead drawHat 2. Run the program and verify that it works.

21 Hour 3: Lab 3.1: Modular Snowman (cont.)
Task 3: Modify the modular program so that it draws a two-headed snowman. Procedure 1. Change the main body of the program so that it calls the drawHead function twice to draw a two- headed snowman. Note that you may need to add or change function parameters to get the desired results.

22 Hour 3: Lab 3.1: Modular Snowman (cont.)
Task 4: Modify the initial, non-modular version so that it draws a two-headed snowman. Procedure 1. Open the version of the program with which you started the lab. 2. Modify the one main function so that it draws two heads instead of one. Be sure to only append to the main function; for this version, do not write any additional functions.

23 BREAK!

24 Hour 4: Lab 3.1: Modular Snowman (cont.)
Did it work? Did the programs all compile and run successfully? Was the modular version of the program easier to modify and extend? Can you identify the advantages of modular programming? When working on the single-function version of the program, did you notice how mistakes, even simple typos, were easily copied and pasted because the code was not being reused? Can you imagine trying to work as part of a team of programmers where you and your team were responsible for maintaining one large, multi-purpose function? How would you work on the program simultaneously? How would you decide who was responsible for what sections of the program?

25 Hour 4: Quiz 3.1: pg 176 Open an and address it to The subject line should be “Quiz 3.1 – Name” Answer the Short Answer questions on page 176 of Starting Out with Games and Graphics in C++. Complete Exercises 1 and 2 in the Algorithm Workbench section on page 176 of Starting Out with Games and Graphics in C++.

26 Hour 4: Answers to Quiz 3.1: pg 176
1. Functions can reduce the duplication of code within a program. If a specific operation is performed in several places in a program, a function can be written once to perform that operation, and then be executed any time it is needed. This is known as code reuse because you are writing the code to perform a task once and then reusing it each time you need to perform the task. 2. A function definition has two parts: a header and a body. The header indicates the starting point of the function, and the body is a list of statements that belong to the function. 3. When the end of the function is reached, the computer jumps back to the part of the program that called the function, and the program resumes execution at that point.

27 Hour 4: Answers to Quiz 3.1: pg 176 (cont.)
 4. A local variable is a variable that is declared inside a function. It belongs to the function in which it is declared, and only statements in the same function can access it. 5. A local variable’s scope usually begins at the variable’s declaration and ends at the end of the function in which the variable is declared. 6. Passing an argument by value means that only a copy of the argument’s value is passed into the parameter variable. Changing the contents of the parameter variable inside the function has no effect on the argument in the calling part of the program. Passing an argument by reference means that the argument is passed into a special type of parameter known as a reference variable. When a reference variable is used as a parameter in a function, it allows the function to modify the argument in the calling part of the program.

28 Hour 4: Answers to Quiz 3.1: pg 176 (cont.)
 Algorithm Workbench #1: void diagonal ( ) { // Get the lower right XY coordinates. int lowerRightX = dbScreenWidth ( ) – 1; int lowerRightY = dbScreenHeight ( ) – 1; // Draw the diagonal line. dbLine (0, 0, lowerRightX, lowerRightY); (Why do we use dbScreenWidth () – 1?) See pg. 44

29 Hour 4: Answers to Quiz 3.1: pg 176 (cont.)
 Algorithm Workbench #1 (cont.): 640 x 480 (0, 0) (639, 0) (0, 479) (639, 479)

30 Hour 4: Answers to Quiz 3.1: pg 176 (cont.)
 Algorithm Workbench #2: void showName (int x, int y) { dbCenterText (x, y, “Jason D. Grose”); }

31 Hour 4: Homework 3.1 Answer the Multiple Choice Review Questions on pages of Starting Out with Games and Graphics in C++. Submit your written answers to me in next week.

32 Hour 4: Next Week Read Chapter 5: Working with Images
Turn in Homework 3.1. your lab .cpp files to


Download ppt "Agenda for Unit 3: Functions"

Similar presentations


Ads by Google