1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.

Slides:



Advertisements
Similar presentations
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Advertisements

1 C++ Functions. // The function computes and returns the gross pay // based on the pay rate and hours. Hours over // 40 will be paid 1.5 times the regular.
Single-Result Functions Section /25/11. Programming Assignment On website.
Chapter 5 Functions.
Chapter 6: User-Defined Functions 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 
Chapter 6: User-Defined Functions I
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
1 CS 1430: Programming in C++. 2 IF Statement if (cond) statement //Next statement if (cond) { statement1 statement2 … } //Next statement.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 8 Arrays.
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
1 CS 1430: Programming in C++. 2 Find Max, Min, Average of m Sections Max, Min and Average of each section Max, Min and Average of all sections together.
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
1 CS 1430: Programming in C++. 2 C++ Loops Sentinel Controlled Count Controlled EOF Controlled.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
CS 1430: Programming in C++ Function Design 1. Good Functions Focusing on one thing Function name tells what it does sqrt(val) pow(base, exp) cin.eof()
1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CS 1430: Programming in C++.
Arrays float Scores[9]; ? index: element // one dimensional array 1.
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.
CS 1428 Final Exam Review. Exam Format 200 Total Points – 60 Points Writing Programs – 45 Points Tracing Algorithms and determining results – 20 Points.
Reference and Value Parameters Reference Parameters (&) The formal parameter receives the address of the actual parameter, and the function can read and.
Chapter 6: User-Defined Functions I
Introduction to Programming
CS 1430: Programming in C++ No time to cover HiC.
CMPT 201 Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
CS 1430: Programming in C++.
CSCI 161: Introduction to Programming Function
CS 1428 Exam II Review.
CS 1430: Programming in C++.
Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope.
Introduction to Functions
CS 1428 Exam II Review.
CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich
CS 1430: Programming in C++ No time to cover HiC.
CS 1430: Programming in C++ No time to cover HiC.
CS 1428 Final Exam Review.
CS 1430: Programming in C++.
CS 1428 Final Exam Review.
Chapter 6: User-Defined Functions I
Programming Languages
Class 4: Repetition Pretest Posttest Counting Flowchart these!
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 1430: Programming in C++.
CS 1430: Programming in C++ No time to cover HiC.
Presentation transcript:

1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute and return the square root } Function Header, Body Receiving value through parameters Function Call root1 = (-coefB + sqrt(Delta)) / (2 * coefA); Function Parameters (Arguments) Formal Parameters Actual Parameters

2 Write a C++ function to find and return the largest value of three integers. Function prototype Name, Type, Parameters, Parameter Types Function Name: Largest or LargestOfThreeInt Your choice and meaningful name Function Type: int Parameters: How many? num1, num2, num3 Data Type of each parameter int

3 Function prototype int Largest(int num1, int num2, int num3);

4 // Function prototype int Largest(int num1, int num2, int num3); // Specify types! // Formal parameters. int main() { int score1, score2, score3, max; cin >> score1 >> score2 >> score3; // Call function to find the highest score max = Largest(score1, score2, score3); // Function call: No types! // Actual parameters. // Different names from formal parameters. cout << "The largest score is " << max; return 0; }

5 Function definition // // The function finds and returns the largest // value of three integers. // int Largest(int num1, int num2, int num3) { // local variable int maxVal; maxVal = num1; if (num2 > maxVal) maxVal = num2; if (num3 > maxVal) maxVal = num3; return maxVal; } // Could be implemented in different ways // Where to get the values of num1, num2, num3? // Actual parameters at function calls.

6 // Function definition // Function Header // Function body int Largest(int num1, int num2, int num3) { int maxVal; maxVal = num1; if (num2 > maxVal) maxVal = num2; if (num3 > maxVal) maxVal = num3; return maxVal; }

7 Programming Rules Every function, except the main function, must have a comment which describes what it does. Other rules on functions.

8 // // Comment block // // Includes // Constants // Function prototype int Largest(int num1, int num2, int num3); int main() { int score1, score2, score3, max; cin >> score1 >> score2 >> score3; max = Largest(score1, score2, score3); cout << "The largest score is " << max; return 0; } // // The function finds and returns the largest // value of three integers. // int Largest(int num1, int num2, int num3) { // local variable int maxVal; … return maxVal; }

9 Function Call and Control Transfer int main() { int score1, score2, score3, max; cin >> score1 >> score2 >> score3; max = Largest(score1, score2, score3); cout << “Max = “ << max; return 0; } int Largest(int num1, int num2, int num3) { int maxVal; maxVal = num1; if (num2 > maxVal) maxVal = num2; if (num3 > maxVal) maxVal = num3; return maxVal; } main() Largest() Function call Passing parameters Returning to main() With return value

10 Tracing in HiC Notes\HiC\Largest.cpp Run menu Watches Add variables Break Points Step Over Trace Into

11 Tracing on Paper Input values: main() Largest() score1 score2 score3 max num1 num2 num3 maxVal ? ? ? ? ? ? ? ?

12 Schedule Quiz4 -1 Submit to HiC Server Due 5 pm Next Monday Test 1 Friday Program 2 Drop your cpp file next Monday Due next Wednesday(better before Test 1) Grace: next Friday Must me to explain!

13 How to Prepare for Test 1 Test 1: Friday 60 Points C++ Statements Tracing C++ Program Other staff Review Notes: 01 – 10 Quizzes Labs Prog1 Prog2

14 C++ Loops Sentinel Controlled Count Controlled EOF Controlled

15 Program 2 Pseudo Code While not done Process one route What kind of loop? EOF Prime Read While not cin.eof() Process one route Read data for next route

16 Program 2 Prime Read: What to read? While not cin.eof() Process one route Read data for next route: What to read? Prime Read: number of deliveries While not cin.eof() Process one route Read data for next route: number of deliveries

17 Program 2: Top-Down Prime Read: numDeliveries While not cin.eof() // Process one route set loopCount to 0 while loopCount < numDeliveries Input data Process data loopCount ++ Read data for next route: numDeliveries

18 Program 2: Bottom-up Hints 2.Work on getting your program to process (i.e., compute the distance between) one pair of consecutive deliveries. Assuming the first delivery for a particular route is at the grid location (3,17), compute the distance from (0,0) to (3,17)! 3.Once your program can process one pair of consecutive deliveries, add a loop to process all of the deliveries of a route IN ORDER. First, compute the distance from (0,0) to (3,17). Then compute the distance from (3,17) to (2,8)! And so on, until reaching the final delivery. 4.Once your program can process an entire delivery route, add an outer loop to process multiple delivery routes. 5.Remember that several variables will need to be reset at the beginning of each delivery route, but other variables, those for computing fleet-level statistics (e.g., the total number of deliveries over all of the delivery routes) should not be reset.