 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!

Slides:



Advertisements
Similar presentations
C Functions. What are they? In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive.
Advertisements

Monday, 11/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Monday, 11/11/02  Questions? HW 04 due today at 5.  Today – Lists and an introduction to searching.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Your First C++ Program Aug 27, /27/08 CS 150 Introduction to Computer Science I C++  Based on the C programming language  One of today’s most.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Monday, 9/23/02, Slide #1 CS 106 Intro to CS 1 Monday, 9/23/02  QUESTIONS??  Today:  Discuss Lab 3  Do Exercises  Introduction to functions  Reading:
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
Wednesday, 10/9/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/9/02  QUESTIONS ??  Today:  Discuss HW #02  Discuss test question types  Review 
CS 201 Functions Debzani Deb.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Guide To UNIX Using Linux Third Edition
Chapter 1 Program Design
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
High-Level Programming Languages: C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 1 Introduction to Computers and C++ Programming Goals: To introduce the fundamental hardware and software components of a computer system To introduce.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
CPS120: Introduction to Computer Science Decision Making in Programs.
C++ Functions. Objectives 1. Be able to implement C++ functions 2. Be able to share data among functions 2.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Algorithms and Programming Functions Lecture 28. Summary of Previous Lecture while statement for statement break statement Nested loops.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Intermediate 2 Computing Unit 2 - Software Development.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Fundamental Programming Fundamental Programming Introduction to Functions.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
User-Written Functions
User Defined Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
Programming Funamental slides
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Functions, Part 1 of 3 Topics Using Predefined Functions
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
CS150 Introduction to Computer Science 1
Presentation transcript:

 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  New files/handouts: SwapTest.cpp

 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

 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

 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.

 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

 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!)

 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

 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

 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

 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!!

 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

 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!!