Download presentation
Presentation is loading. Please wait.
1
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters
2
Ch 6 / Foil 2 l Includes description of the interface and the function body n Interface –Similar to a function prototype, but parameters names are required n Body –Statement list with curly braces that comprises its actions –Return statement to indicate value of invocation Function Definition
3
Ch 6 / Foil 3 float CircleArea (float r) { const float Pi = 3.1415; return Pi * r * r; } Function Definition Function body Return statement Local object definition Formal parameter Return type Function name
4
Ch 6 / Foil 4 Function Invocation cout << CircleArea(MyRadius) << endl; Invocation is part of a larger expression. To process the invocation, the function that contains the insertion statement is suspended and CircleArea() does its job. The insertion statement is then completed using the value supplied by CircleArea(). Actual parameter
5
Ch 6 / Foil 5 Simple Programs l Single file n Include statements n Using statements n Function prototypes n Function definitions l Functions use value parameter passing n Also known as pass by value or call by value –The actual parameter is evaluated and a copy is given to the invoked function
6
Ch 6/ Foil 6 #include using namespace std; float CircleArea(float r); // main(): manage circle computation int main() { cout << "Enter radius: "; float MyRadius; cin >> MyRadius; float Area = CircleArea(MyRadius); cout << "Circle has area " << Area; return 0; } // CircleArea(): compute area of radius r circle float CircleArea(float r) { const float Pi = 3.1415; return Pi * r * r; }
7
Ch 6 / Foil 7 Value Parameter Passing Rules l Formal parameter is created on function invocation and it is initialized with the value of the actual parameter l Changes to the formal parameter do not affect the actual parameter l Whenever a formal parameter is referenced, the value used for the formal parameter is the one stored in the current activation record for the function l There is a new activation record for each invocation l The name of the formal parameter is only known within its function l The formal parameter is destroyed when the function invocation completes l Activation record memory is automatically released at completion
8
Ch 6/ Foil 8
9
Ch 6 / Foil 9 PromptAndRead() // PromptAndRead(): prompt and extract next // integer int PromptAndRead() { cout << "Enter number (integer): "; int Response; cin >> Response; return Response; }
10
Ch 6 / Foil 10 Max() // Max(): return larger of a and b int Max(int a, int b) { if (a < b) { return b; } else { return a; }
11
Ch 6 / Foil 11 Min() // Min(): return smaller of a and b int Min(int a, int b) { if (a > b) { return b; } else { return a; }
12
Ch 6 / Foil 12 Sum() // Sum(): compute sum of integers in a... b int Sum(int a, int b) { int Total = 0; for (int i = a; i <= b; ++i) { Total += i; } return Total; }
13
Ch 6 / Foil 13 Problem l Definition n Input two numbers that represent a range of integers and display the sum of the integers that lie in that range l Design n Prompt user and read the first number n Prompt user and read the second number n Determine the smaller of the two numbers n Determine the larger of the two numbers n Calculate the sum of integers in the range smaller...larger by adding in turn each integer in that range n Display the sum
14
#include using namespace std; int PromptAndRead(); int Min(int a, int b); int Max(int a, int b); int Sum(int a, int b); int main() { int FirstNumber = PromptAndRead(); int SecondNumber = PromptAndRead(); int Smaller = Min(FirstNumber, SecondNumber); int Larger = Max(FirstNumber, SecondNumber); int RangeSum = Sum(Smaller, Larger); cout << "The sum from " << Smaller << " to " << Larger << " is " << RangeSum << endl; return 0; } Ch 6/ Foil 14
15
// Min(): return smaller of a and b int Min(int a, int b) { if (a > b) return b; else return a; } // Max(): return larger of a and b int Max(int a, int b) { if (a < b) return b; else return a; } Ch 6/ Foil 15
16
// PromptAndRead(): prompt & extract next integer int PromptAndRead() { cout << "Enter number (integer): "; int Response; cin >> Response; return Response; } // Sum(): compute sum of integers in a... b int Sum(int a, int b) { int Total = 0; for (int i = a; i <= b; ++i) { Total += i; } return Total; } Ch 6/ Foil 16
17
Ch 6 / Foil 17 Blocks and Local Scope l A block is a list of statements within curly braces l Blocks can be put anywhere a statement can be put l Blocks within blocks are nested blocks l An object name is known only within the block in which it is defined and in nested blocks of that block l A parameter can be considered to be defined at the beginning of the block corresponding to the function body
18
Ch 6 / Foil 18 Local Object Manipulation void f() { int i = 1; cout << i << endl; // insert 1 { int j = 10; cout << i << j << endl; // insert 1 10 i = 2; cout << i << j << endl // insert 2 10 } cout << i << endl; // insert 2 cout << j << endl; // illegal }
19
Ch 6 / Foil 19 Name Reuse l If a nested block defines an object with the same name as enclosing block, the new definition is in effect in the nested block
20
void f() { { int i = 1; cout << i << endl; // insert 1 { cout << i << endl; // insert 1 char i = 'a'; cout << i << endl; // insert a } cout << i << endl; // insert 1 } cout << i << endl; // illegal insert } Ch 6/ Foil 20
21
Ch 6 / Foil 21 Global Scope l Objects that are not defined within a block are global objects l A global object can be used by any function in the file that is defined after the global object l It is best to avoid programmer-defined global objects n Exceptions tend to be important constants l Global objects with appropriate declarations can even be used in other program files cout, cin, and cerr are global objects that are defined in by the iostream library l Local objects can reuse a global object's name l Unary scope operator :: can provide access to global object even if name reuse has occurred
22
Ch 6 / Foil 22 Example int i = 1; int main f() { cout << i << endl; // insert 1 char i = 'a'; cout << i << endl; // insert a ::i = 2; cout << i << endl; // insert a cout << ::i << endl; // insert 2 return 0; }
23
Ch 6 / Foil 23 Integrating Quadratic Polynomials l A polynomial f(x) of degree n is a function whose value is the sum of n+1 terms where the terms are of the form a i x i, 0 i n, with the restriction that a n is not 0. a n x n + a n–1 x n–1 + … + a 2 x 2 + a 1 x + a 0 l The area under the curve along the x-axis interval (s, t) for f(x) is a n ( s n +1 - t n+1 ) + a n–1 ( s n - t n ) + … + a 1 (t 2 - s 2 )/2 + a 0 (t - s) l For a quadratic polynomial (degree 2) this equation reduces a 2 (t 3 - s 3 )/3 + a 1 (t 2 - s 2 )/2 +a 0 (t - s)
24
Ch 6 / Foil 24 QuadraticArea() double QuadraticArea(double a2, double a1, double a0, double s, double t) { double term2 = a2*(t*t*t - s*s*s)/3.0; double term1 = a1*(t*t - s*s)/2.0; double term0 = a0*(t - s); return term2 + term1 + term0; }
25
#include // program 6.3 #include using namespace std; double QuadraticArea(double a2, double a1, double a0, double s, double t); int main() { cout << "Enter quadratic coefficients (n n n): "; double a2, a1, a0; cin >> a2 >> a1 >> a0; cout << "Enter interval of interest (n n): "; double s, t; cin >> s >> t; double Area = QuadraticArea(a2, a1, a0, s, t); cout << "The area of quadratic polynomial " << a2 << "x*x + " << a1 << "x + " << a0 << " for (" << s << ", " << t << ")" << " is " << QuadraticArea(a2, a1, a0, s, t) << endl; }
26
Ch 6 / Foil 26 Recursion l Recursion is the ability of a function to call itself. l Consider the mathematical function n! n! = n * (n-1) … * 2 * 1 is not mathematically precise because we use an ellipsis (…). l Consider the following formal definition n n! = 0, if n = 0 n n! = n * (n-1)!, if n > 0 –The factorial function is defined in terms of itself
27
Ch 6 / Foil 27 Consider #include using namespace std; int main() { cout << "Enter a positive integer: "; int n; cin >> n; cout << n << "! = " << Factorial(n) << endl; return 0; }
28
Ch 6 / Foil 28 Using int Factorial(int n) { if (n == 0) { return 1; } else { return n * Factorial(n-1); } l C++ function mirrors the mathematical factorial definition If the value of n is 0, the value 1 is returned. Otherwise, the product of n and Factorial(n-1) is returned.
29
Ch 6 / Foil 29 Recursion Visualization Consider cout << n ! = " << Factorial(3) << endl;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.