Download presentation
Presentation is loading. Please wait.
Published byAnastasia Murphy Modified over 9 years ago
1
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters // More examples float sqrt(float x); char myFunction(float param1, int param2, char param3);
2
2 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); // Do not include type for actual parameters!
3
3 Function Calls // Function prototype float GrossPay(float payRate, float hours) cin >> hours >> rate; // Function calls gross = GrossPay(rate, hours); gross = GrossPay(hours, rate); // No Error // But incorrect result
4
4 Function Calls // Function prototype float sqrt(float x); // Function calls cout << “Square Root: ” << sqrt(delta); sqrt(delta); // Error: Must use the return value result = sgrt(delta); // Good!
5
5 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 charVal = myFunction(50.57, 48, “M”); // ValidInvalid myFunction(50.57, 48, ‘M’); // ValidInvalid cout << myFunction(50.57, 48, ‘M’); // ValidInvalid
6
6 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 float, float, float void DisplayResult(float avg, float max, float min);
7
7 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!
8
8 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; }
9
9 Functions without Parameters void UserInstruction(); int main() { float score, avg, highest, lowest; int numScores, loopCount = 0; UserInstruction(); // input // process DisplayResult(avg, highest, lowest); return 0; }
10
10 // 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; }
11
11 Example void DisplayMenu(); int main() { … DisplayMenu(); … } void DisplayMenu() { cout << “\nM or m to convert Meters to Inches” << … return; }
12
12 Scope of Variables The region of code where it is legal to reference (use) an identifier. Local Scope Global Scope Class Scope
13
13 Code Block Between a pair of matching braces The body of a function int main() { int alpha = 10; // A block for if statement if (alpha > 3) { int n; cin >> n; alpha += 3; } return 0; }
14
14 Local Scope The scope of an identifier declared inside a block extends from the point of declaration to the end of that block. int main() { int alpha = 10; // A code block if (alpha > 3) { int num; cin >> num; alpha += num; } cout << “num = ” << num; // Run time error! return 0; }
15
15 Global Scope The scope of an identifier declared outside all functions (and classes) extends from the point of declaration to the end of the entire source file. Programming Rules: No global variables!
16
16 Class Scope Later this semester!
17
17 Scope of Function Parameters Formal parameters Local scope Same as local variable Cannot reference it outside the function Receive values on function call Actual parameters (no global variables) Local scope Cannot reference it inside the called function
18
18 Example float DoIt(int num, char op); int main() { int base; float result; char choice; cout << “Enter a number: ”; cin >> base; cout << “C for Cube and S for Square Root: ”; cin >> choice; while (choice != ‘C’ && choice != ‘S’) { cout << “C for Cube and S for Square Root: ”; cin >> choice; } result = DoIt(base, choice); cout << “The result: ” << result; return 0; } // ---------------------------- // Precondition: op is ‘C’ or ‘S’ // Postcondition: the cube of // num is computed when op is // ‘C’, and square root of num // is computed when op is ‘S’. // ------------------------------ float DoIt(int num, char op) { if (op == ‘C’) result = pow(num, 3); else result = sqrt(num); return result; } // What is wrong? // Result not declared in the // function!
19
19 Precondition and Postcondition int DoIt(int num, char op); int main() { int base; float result; char choice; cout << “Enter a number: ”; cin >> base; cout << “C for Cube and S for Square: ”; cin >> choice; while (choice != ‘C’ && choice != ‘S’) { cout << “C for Cube and S for Square: ”; cin >> choice; } result = DoIt(base, choice); cout << “The result: ” << result; return 0; } // ------------------------------ // Precondition: op is ‘C’ or ‘S’ // Postcondition: the cube of // num is computed when op is // ‘C’, and square root of num // is computed when op is ‘S’. // ------------------------------ int DoIt(int num, char op) { float result; if (op == ‘C’) result = pow(num, 3); else result = sqrt(num); return result; } // The two variables // result have the same // name, but different!
20
20 Parameter Names Meaningful names Formal and actual parameters can have the same name They are different variables in different scopes Normally they have different names
21
21 Lifetime of a Variable Lifetime The period of time during program execution when an identifier has memory allocated to it. Automatic variables A variable for which memory is allocated and deallocated when control enters and exits the block it is declared. Static variables A variable for which memory remains allocated throughout the execution of the entire program.
22
22 Program 2 Due 9:30 PM Today Grace date Friday Send email to me Style Lab 4: Thursday
23
Exercise: Tracing Function Call Exercise\GrossPayFunction.doc 23
24
24 Tracing on GrossPay Input values: 10 45 main() GrossPay() hours rate gross payRate hours pay ? ? ? ? ? ? 45 10 10 45 475
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.