Download presentation
Presentation is loading. Please wait.
Published byBrice Booth Modified over 8 years ago
1
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
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
3 Function prototype int Largest(int num1, int num2, int num3);
4
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
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
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
7 Programming Rules Every function, except the main function, must have a comment which describes what it does. Other rules on functions.
8
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
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
10 Tracing in HiC Notes\HiC\Largest.cpp Run menu Watches Add variables Break Points Step Over Trace Into
11
11 Tracing on Paper Input values: 53 48 60 main() Largest() score1 score2 score3 max num1 num2 num3 maxVal ? ? ? ? ? ? ? ? 53 48 60 53 60
12
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 email me to explain!
13
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
14 C++ Loops Sentinel Controlled Count Controlled EOF Controlled
15
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
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
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
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.