Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!

Similar presentations


Presentation on theme: " Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!"— Presentation transcript:

1  Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!  Function prototypes  Reference parameters  Reading: Finish Ch. 3, Ch. 4, Sec 5.1-5.2  New files/handouts: SwapTest.cpp

2  Monday, 9/30/02, Slide #2 Function Libraries  C++ has a number of library files, containing collections of predefined functions for special purposes:  : Objects and functions for doing input and output to the console  : Functions to do mathematical calculations  : Objects and functions for input and output to files  : Objects and functions to format output  : Objects and functions for working with character strings ... and numerous others

3  Monday, 9/30/02, Slide #3 The Program Design Process  We now have all the basic tools we need to write programs (although we’ll learn more advanced tools too)  Chapter 4 discusses the author’s view of the process of designing and maintaining programs

4  Monday, 9/30/02, Slide #4 From Problem to Program  1. Read the problem carefully, until you are sure you understand what is being asked. You may need to try out some examples first.  2. Design a basic algorithm: A sequence of main steps to follow to solve the problem. Each step may be quite complex. This algorithm will probably be in natural language, on paper.  3. Start to think about what objects (variables) will be needed, and what their types and names will be.

5  Monday, 9/30/02, Slide #5 Top-down design  Top-down design is the process of going from a basic algorithm with only a few, but complex, steps to a final algorithm with many, but simple, steps: The final algorithm will either be in C++ or easily translatable to C++. Intermediate stage algorithms will be expressed in “pseudocode.”  At each stage, treat each step of the current algorithm as a new, smaller problem to solve:  Understand what’s required  Break it down into simpler steps  Think about what objects are needed

6  Monday, 9/30/02, Slide #6 The Software Life Cycle  1. Problem Analysis  2. Algorithm/data structure design  3. Coding: translation into computer code  4. Testing: Rigorous testing of all possible cases that might occur, being sure that all lines of code are tested. Design (on paper!) a complete set of test data before beginning to test!  5. Maintenance: Revising code to meet changing problem requirements (return to step 1!)

7  Monday, 9/30/02, Slide #7 Placement of Function Definitions: Method 1, Before main() int Max (int A, int B) {if (A > B) return A; else //B >= A return B; } void main() { int Num1, Num2; cin >> Num1 >> Num2; cout << Max(Num1, Num2); }  Here we place the entire definition of Max() before main()  Plus: When compiled, main() “knows” that Max() exists  Minus: User has to scan through whole function definition to get to main() -- violates “top-down” idea

8  Monday, 9/30/02, Slide #8 Placement of Function Definitions: Method 2, Prototype before main(), definition after main() int Max (int A, int B) ; //returns maximum of A and B void main() { int Num1, Num2; cin >> Num1 >> Num2; cout << Max(Num1, Num2); } int Max (int A, int B) {if (A > B) return A; else //B >= A return B; }  Here we place the definition of Max() after main()  Must also place prototype (function heading plus semicolon) -- so main() knows that Max() exists  Plus: Lets programmer see what functions main() calls without having to scan past code -- top-down design!  Minus: One extra line of typing

9  Monday, 9/30/02, Slide #9 More on parameters: Reference parameters  Some functions are designed to return multiple values, but are only permitted one return value.  Example (also in text): A function that swaps the values in two objects  What happens if we try to do this with value parameters? See next slide! Swap(A, B) BEFORE IN main(): int A = 5 int B = 10 AFTER IN main(): int A = 10 int B = 5

10  Monday, 9/30/02, Slide #10 Trying to swap with value parameters  Here we would really like the parameters first and second to refer to the same memory location as the arguments A and B! void Swap (int first, int second); void main() {int A = 5; int B = 10; Swap (A, B); cout << “A = “ << A << endl; cout << “B = “ << B << endl; } void Swap (int first, int second) {int Temp = first; first = second; second = Temp; } Trace this code!!

11  Monday, 9/30/02, Slide #11 Reference parameters  In order to permit functions to return multiple values (such as the two swapped values in the example) C++ uses another kind of parameter called reference parameter. In the function heading (of both prototype and definition) we write:  void Swap (int& A, int& B);  The ampersand tells the compiler that this parameter should refer to the same memory location as the corresponding argument

12  Monday, 9/30/02, Slide #12 Swapping with reference parameters  Function call in main() is unchanged  When we use reference parameters, arguments must be object names void Swap (int& A, int& B); void main() {int A = 5; int B = 10; Swap (A, B); cout << “A = “ << A << endl; cout << “B = “ << B << endl; } void Swap (int& first, int& second) {int Temp = first; first = second; second = Temp; } Trace this code!!


Download ppt " Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!"

Similar presentations


Ads by Google