Agenda for Unit 3: Functions

Slides:



Advertisements
Similar presentations
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Advertisements

Procedural programming in Java
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Methods
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: Functions Starting Out with C++ Early Objects
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Procedural programming in Java Methods, parameters and return values.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Objectives: How to define and call functions. Function declarations and how they differ from function definitions. How arguments are passed to functions.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions + Overloading + Scope
User-Written Functions
Chapter 6: User-Defined Functions I
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
Deitel- C:How to Program (5ed)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Methods.
Functions.
User-Defined Functions
User Defined Functions
Chapter 4 void Functions
6 Chapter Functions.
Functions Chapter 9 Copyright © 2008 W. W. Norton & Company.
A function is a group of statements that exist within a program for the purpose of performing a specific task. We can use functions to divide and conquer.
CS149D Elements of Computer Science
Based on slides created by Bjarne Stroustrup & Tony Gaddis
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Predefined Functions Revisited
Agenda for Unit 5: Control Structures
Agenda for Unit 1: Introduction of Computers and Games Graphics
Standard Version of Starting Out with C++, 4th Edition
Agenda for Unit 4: Graphics
Agenda for Unit 8: Defining Arrays in Game Programs
Presentation transcript:

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.

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

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

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

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

Ready?

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%

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

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

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 = 10 + 14; 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 ( );

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

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

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

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

BREAK!

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

BREAK!

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

Hour 3: Lab 3.1: Modular Snowman (Programming Exercise #1 on pages 176-177 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.

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.

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.

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.

BREAK!

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?

Hour 4: Quiz 3.1: pg 176 Open an email and address it to jasongrose@gmail.com. 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++.

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.

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.

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

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)

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”); }

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

Hour 4: Next Week Read Chapter 5: Working with Images Turn in Homework 3.1. Email your lab .cpp files to jasongrose@gmail.com