J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Functions Computational assistants.

Slides:



Advertisements
Similar presentations
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Advertisements

Iterative Constructs – chapter 4 pp 189 to 216 This lecture covers the mechanisms for deciding the conditions to repeat action. Some materials are from.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Templates and Polymorphism Generic functions and classes.
Function basics – pp Computational assistants – improves clarity and enable software reuse. JPC and JWD © 2002 McGraw-Hill, Inc.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
 Wednesday, 10/16/02, Slide #1 CS106 Introduction to CS1 Wednesday, 10/16/02  QUESTIONS??  Today:  Return and discuss Test #1  Input from and output.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
Chapter 7 Functions.
1 Chapter 7 Functions. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
Stream Handling Streams - means flow of data to and from program variables. - We declare the variables in our C++ for holding data temporarily in the memory.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Modifying objects Operators and Expressions.
Modifying objects Operators and Expressions JPC and JWD © 2002 McGraw-Hill, Inc.
1 Programming in C++ Dale/Weems/Headington Chapter 7 Functions.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
COMPUTER PROGRAMMING. A Typical C++ Environment Phases of C++ Programs: 1- Edit 2- Preprocess 3- Compile 4- Link 5- Load 6- Execute Loader Primary Memory.
File I/O ifstreams and ofstreams Sections 11.1 &
1 CS161 Introduction to Computer Science Topic #13.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Chapter 8 Functions. Chapter 8 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task l Using Function Arguments.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 Functions. 2 Chapter 7 Topics  Writing a Program Using Functional Decomposition  Writing a Void Function for a Task  Using Function Arguments and.
1 COMS 261 Computer Science I Title: String Class Date: October 3, 2005 Lecture Number: 14.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Chapter 3: Input/Output
Vectors One-Dimensional Containers. Problem A file contains a sequence of names and scores: Ann92 Bob84 Chris89... Using OCD, design and implement a program.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
GE 211 Programming in C ++ Dr. Ahmed Telba Room :Ac -134
C++: Functions, Program Compilation, Libraries Modified from CS101 slides, which are by JPC and JWD © 2002 McGraw-Hill, Inc.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Chapter 8 Functions.
Introduction to C++ (Extensions to C)
ifstreams and ofstreams
Basic Input and Output Operations
Copyright © 2003 Pearson Education, Inc.
Parallel Arrays Parallel array =>Two or more arrays with the same number of elements used for storing related information about a collection of data. Example:
COMS 261 Computer Science I
Intro to Programming Week # 4 Control Structure Lecture # 8
Chapter 5 Function Basics
Programming Funamental slides
Programming with Data Files
Chapter 3: Input/Output
Wednesday 09/23/13.
Chapter 3 Input output.
Programs written in C and C++ can run on many different computers
COMS 261 Computer Science I
Iterative Constructs Mechanisms for deciding under what conditions an action should be repeated JPC and JWD © 2002 McGraw-Hill, Inc.
CHAPTER 4 File Processing.
COMS 261 Computer Science I
ifstreams and ofstreams
COMS 261 Computer Science I
Chapter 8 Functions.
Modifying Objects Assignment operation Assignment conversions
Presentation transcript:

J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Functions Computational assistants

Ch 5 / Foil 2 Functions l Previous examples n Programmer-defined functions – main() – ApiMain() n Library-defined functions – cin.get() –string member functions size(), find() and substr() – RectangleShape member function Draw() – SimpleWindow member function Open() l Some good advice n Don’t reinvent the wheel! There are lots of libraries out there

Ch 5 / Foil 3 Terminology l A function is invoked (called, executed) by a function call and produces a return value l A function call specifies n The function name –Function name indicates what function is to be called n The values or actual parameters to be used in the invocation –The values are the actual information that the function requires to perform its task

Invocation Library header file Ch 5/ Foil 4 #include using namespace std; #include int main() { cout << "Quadratic coefficients: "; double a, b, c; cin >> a >> b >> c; if ((a != 0) && ((b*b - 4*a*c) > 0)) { double radical = sqrt(b*b - 4*a*c); double root1 = (-b + radical) / (2*a); double root2 = (-b - radical) / (2*a); cout << "Roots: " << root1 << " " << root2; } else { cout << "Does not have two real roots"; } return 0; }

Ch 5 / Foil 5 Invocation and Execution Process l Flow of control is temporarily transferred to the invoked function n Next statement executed is the first one in the invoked function l Correspondence established between the actual parameters used in the invocation with the formal parameters in the definition. Value of b*b - 4*a*c  first actual parameter n Parameters are maintained in the invocation activation record l Invoked function is executed l Flow of control returns to the invocation statement l The return value of the invoked function is used in the invocation expression

Ch 5 / Foil 6 Activation Records l Fresh activation record is created for each invocation of a function. Even function main(), which is invoked by the operating system, has an activation record. l The activation record for a function is large enough to store n Values associated with each object that is defined within the function n Memory to temporarily hold the return value n Depending upon the compiler –A pointer to the current statement being executed –A pointer to the invoking statement

Ch 5/ Foil 7

Ch 5 / Foil 8 Libraries l Library n Collection of functions, classes, and objects grouped by commonality of purpose n Include statement provides access to the names and descriptions of the library components n Linker connects program to actual library definitions l Previous examples n String: STL’s string class n Graphics: EzWindows

Ch 5 / Foil 9 Some Helpful Standard Libraries l fstream n File stream processing l assert n C-based library for assertion processing l iomanip n Formatted input/output (I/O) requests l ctype n C-based library for character manipulations l math n C-based library for trigonometric and logarithmic functions l Note n C++ has many other libraries

Ch 5 / Foil 10 Library Header Files l Describes library components l Typically contains n Function prototypes –Interface description n Class definitions l Sometimes contains n Object definitions –Example: cout and cin in iostream l Typically does not contain function definitions n Definitions are in source files n Access to compiled versions of source files provided by a linker

Ch 5 / Foil 11 Basic Translation Process

#include // file stream library #include using namespace std; int main() { ifstream fin("mydata.nbr"); int ValuesProcessed = 0; float ValueSum = 0; float Value; while (fin >> Value) { ValueSum += Value; ++ValuesProcessed; } if (ValuesProcessed > 0) { ofstream fout("average.nbr"); float Average = ValueSum / ValuesProcessed; fout << "Average: " << Average << endl; } else cerr << "No list to average" << endl; return 0; }

ifstream sin("in1.txt"); // extract from in1.txt ofstream sout("out1.txt"); // insert to out1.txt string s; while (sin >> s) { sout << s << endl; } sin.close(); // done with in1.txt sout.close(); // done with out1.txt sin.open("in2.txt"); // now extract from in2.txt sout.open("out2.txt", // now append to out2.txt (ios_base::out | ios_base::app)); while (sin >> s) { sout << s << endl; } sin.close();// done with in2.txt sout.close();// done with out2.txt

Ch 5 / Foil 14 Function Prototypes l Before the function can appear in an invocation its interface must be specified n Prototype or complete definition int Max(int a, int b)

Ch 5 / Foil 15 Parameters l Associations n The actual parameters are associated with the formal parameters based on position –First actual parameter is bound to first formal parameter –Second actual parameter is bound to second formal parameter –And so on n The names of the actual and formal parameters are immaterial in this mapping

#include using namespace std; #include int main() { cout << "Principal amount: "; double StartingAmount; cin >> StartingAmount; cout << "Interest rate (%): "; double InterestRate; cin >> InterestRate; cout << "Years of deposit: "; double Years; cin >> Years;

double EndingAmount = StartingAmount * pow(1+InterestRate/100.0, Years); cout << "Principal " << StartingAmount << " when compounded annually for " << Years << "\nyears at " << InterestRate << "% produces " << EndingAmount << endl; return 0; }

#include using namespace std; int main() { int Numerator; cout << "Enter numerator: "; cin >> Numerator; int Denominator; cout << "Enter denominator: "; cin >> Denominator; assert(Denominator); // really should be if test int Ratio = Numerator / Denominator; int Remainder = Numerator % Denominator; cout << Numerator << "/" << Denominator << " = " << Ratio << " with remainder " << Remainder; return 0; }