Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich

Similar presentations


Presentation on theme: "1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich"— Presentation transcript:

1 1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich YangQ@uwplatt.edu

2 Test 1 2

3 Good Luck! Test2 and Test3 more difficult! Final exam is 2 hours and more than 10 pages! Don’t want to lose points on Progs and Labs! How can I do better later in the semester? 3

4 4 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters // More examples // What’s the function type? float sqrt(float x); // What’s the function type? char myFunction(float param1, int param2, char param3);

5 5 Function Calls // Function prototype int Largest(int num1, int num2, int num3); // Function calls max = Largest(score1, score2, score3); // Must follow the prototype! // actual parameters are different from formal parameters max = Largest(int score1, int score2, int score3); // OK? // Do not include type for actual parameters!

6 6 Function Calls // Function prototype float GrossPay(float payRate, float hours) cin >> hours >> rate; // Function calls gross = GrossPay(rate, hours); gross = GrossPay(hours, rate); // OK? // No Error // But incorrect result

7 7 Function Calls // Function prototype float sqrt(float x); // Function calls cout << “Square Root: ” << sqrt(delta); sqrt(delta); // OK // Lose points! result = sqrt(delta); // Good! Must use the return value!

8 8 Function Calls // Function prototype char myFunction(float param1, int param2, char param3); char charVal; charVal = myFunction(50.57, 48, ‘M’); // ValidInvalid charVal = myFunction(50.57, 48); // ValidInvalid charVal = myFunction(49, 50.57, ‘M’); // ValidInvalid // Correct Incorrect charVal = myFunction(50.57, 48, “M”); // ValidInvalid myFunction(50.57, 48, ‘M’); // ValidInvalid cout << myFunction(50.57, 48, ‘M’); // ValidInvalid

9 9 VOID Functions Write a C++ function to display max, min and average. Function name DisplayResult Function type void (does not return any value) Function parameters How many? max, min, avg Type of each parameters int, int, float double, double, double float, float, float void DisplayResult(float avg, float max, float min);

10 10 VOID Functions void DisplayResult(float avg, float max, float min); int main() { float score, avg, highest, lowest; int numScores, loopCount = 0; // input scores // Compute avg, highest and lowest // output results DisplayResult(avg, highest, lowest); return 0; } // Decompose a program into smaller functions // Programming Rule: each function can have at most 30 lines! // (function body including braces and all lines inside)

11 11 VOID Functions void DisplayResult(float avg, float max, float min); // Programming Rules: Each function must have a description! // ------------------------------------------------------------ // The function displays avg, max and min in required format. // ------------------------------------------------------------ void DisplayResult(float avg, float max, float min) { cout << fixed << showpoint << setprecision(2); cout << endl << endl << "The average score: " << setw(8) << avg << “.\n” << "The highest score: " << setw(8) << max << “.\n” << "The lowest score : " << setw(8) << min; // return control, no value return; }

12 12 Functions without Parameters void UserInstruction(); void DisplayResult(float avg, float max, float min); // another function prototype int main() { float score, avg, highest, lowest; int numScores, loopCount = 0; UserInstruction(); // input and process scores // Could call another function! DisplayResult(avg, highest, lowest); return 0; }

13 13 // Programming Rules: Each function must have a description! // ----------------------------------------------------- // The function displays user instructions. // ----------------------------------------------------- void UserInstruction() { cout << endl << endl << "The program process all scores of one section " << endl << "of a class. The first input is the number of " << endl << "scores. All scores must be in the range of " << endl << "0.0 and 60.0, inclusive." << endl << endl; return; }

14 14 Example void DisplayMenu(); int main() { … DisplayMenu(); … } void DisplayMenu() { cout << “\nM or m to convert Meters to Inches” << … return; }

15 15 Schedule Program 2 –Due 10 PM Tuesday –Grace date Friday –Requirements No functions! No arrays! No classes! –Style Quiz4-1: Today by 10 pm Lab 4: Tuesday for 3 points

16 Using Input File Lab4.txt 16

17 Program 2 Do It Yourself! Getting Help! Plagiarism Checking 17


Download ppt "1 CS1430: Programming in C++ Section 2 Instructor: Qi Yang 213 Ullrich"

Similar presentations


Ads by Google