Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.

Similar presentations


Presentation on theme: "1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function."— Presentation transcript:

1 1 CS 1430: Programming in C++

2 Quiz 1 2

3 3 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

4 4 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 but must be meaningful! Function Type: int Parameters: How many? num1, num2, num3 Data Type of each parameter int

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

6 6 // 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; }

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

8 8 // 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; }

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

10 10 // -------------------------------------------- // 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; }

11 11 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

12 12 Tracing in VS J:\Public_html\CS143\Labs\LargestByFunction Break Points Locals Autos Debug menu Start Debugging F5 Step Over Trace Into Largest Returned

13 13 Tracing on Paper Input values: 53 48 60 main() Largest() score1 score2 score3 max num1 num2 num3 maxVal ? ? ? ? ? ? ? ? 53 48 60 53 60

14 14 Schedule Quiz4 -1 Submit to the Grader Due 10 pm Next Monday Lab 4 Test 1 Friday Program 2 Due next Tuesday (better before Test 1) Grace: next Friday

15 15 Using Input File Lab 4 Program 2 How to do it? –Lab4 description

16 16 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

17 DO ALL QUIZZES! 17

18 STAR PROGRAMS EARLY! 18

19 DO NOT MISS CLASSES! 19


Download ppt "1 CS 1430: Programming in C++. Quiz 1 2 3 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function."

Similar presentations


Ads by Google