chap06 - Chapter 6 Chapter Programmer-defined defined Functions Functions Development of simple functions using value and reference parameters Key

chap06 - Chapter 6 Chapter Programmer-defined defined...

This preview shows page 1 - 12 out of 59 pages.

Chapter 6 : Chapter 6 : Programmer Programmer - - defined defined Functions Functions
Image of page 1
Development of simple functions using value and reference parameters
Image of page 2
Key Concepts: Key Concepts: programmer-defined function invocation and flow of control parameters prototypes activation records return statement local object scope global objects name reuse header file (*.h) implementation file (*.cpp) standard class ostringstream standard class istringstream class Label Standard Template Library ( STL ) reference parameters constant parameters default parameters function overloading function overloading resolution side effects recursion
Image of page 3
Function Definition Includes description of the interface and the function body Interface Similar to a function prototype, but parameters’ names are required Body Statement list with curly braces that comprises its actions Return statement to indicate value of invocation { //the function body goes here . . . . . . } FunctionType FunctionName ( ParameterList ) Function interface Function body
Image of page 4
Function Definition { const float Pi = 3.1415; return (Pi * r * r); } float CircleArea (float r) Function body Return statement Local object definition Formal parameter Function name Return type
Image of page 5
Function Invocation Actual parameter cout << CircleArea( fMyRadius ) << endl; 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() .
Image of page 6
Call-by-value Functions use value parameter passing Also known as pass by value or call by value The actual parameter is evaluated and a copy is given to the invoked function Actual Parameters Formal Parameters
Image of page 7
Call-by-value Advantages of call-by-value The isolation between formal parameter and actual parameter. That is changes to formal parameter do not affect actual parameter. Have no side effects We should use call-by-value whenever possible. But … Are there disadvantages of call-by-value ?
Image of page 8
#include <iostream> using namespace std; float CircleArea(float r); //function prototype // main(): manage circle computation int main() { cout << "Enter radius: "; float fMyRadius; cin >> fMyRadius; float fArea = CircleArea( fMyRadius ); cout << "Circle has area " << fArea; return 0; } // CircleArea(): compute area of radius r circle float CircleArea(float r) { const float Pi = 3.1415; return Pi * r * r; }
Image of page 9
Call-by-value Rules Formal parameter is created on function invocation and it is initialized with the value of the actual parameter Changes to formal parameter do not affect actual parameter Reference to a formal parameter produces the value for it in the current activation record New activation record for every function invocation Formal parameter name is only known within its function Formal parameter ceases to exist when the function completes Activation record memory is automatically released at function completion
Image of page 10
Call-by-value information stream Information to function can come from parameters or an input stream Information from function can come through a return value or an output stream Return value Function Input stream data Output stream data Parameters
Image of page 11
Image of page 12

You've reached the end of your free preview.

Want to read all 59 pages?

  • Winter '03
  • Pro.Lei
  • C++ Programming, Subroutine, formal parameter,