Lecture Notes for SE 598, Data Structures and Algorithms

31 May 2000, C++ Functions


  1. functions

    1. encapsulates transformations on data structures

  2. default argument values -

    1. formal parameters may include a default value

      1. for example, int open(string name, string mode = "r")

    2. actual parameters corresponding to formal parameters with defaults may be omitted in function calls

      1. open("mbox", "r")

      2. open("mbox")

      3. open("mbox", "w")

    3. any formal parameter with defaults must appear after every formal parameter without defaults

      1. wrong: int set_time(int hour = 0, int minute, int second = 0)

      2. right: int set_time(int hour, int minute, int second = 0)

      3. right: int set_time(int hour = 0, int minute = 0, int second = 0)

    4. default formal parameter values interact with default object constructors

  3. reference parameters

    1. call-by-value vs. call-by-reference

      1. call-by-value passes a copy of the actual parameter

      2. call-by-reference passes the address of the actual parameter

      3. call-by-value is safer than call-by-reference

      4. call-by-value is less efficient than call-by-reference

    2. pointers vs. references

      1. in C, call-by-reference is implemented using the pointer-to * and address-of & operators

        1. definition: void swap(int * ap, int * bp) { ... }

        2. call: swap(&i, &j)

      2. C++ can also use the reference type to implement call-by-reference

        1. definition: void swap(int & a, int & b) { ... }

        2. call: swap(i, j)

    3. reference parameters

      1. reference-type call-by-reference is preferred over address-of and pointer-to call-by-reference

        1. eliminating * and & makes things easier and simpler

    4. constant reference parameters

      1. some measure of call-by-value security can be regained by using constant reference formal parameters

        1. void search(const tree_node & root, int x) { ... }

        2. however - const-ness can be cast away

    5. stl makes heavy use of reference and const reference types in parameters

    6. const reference and reference types are different; confusing them can lead to confusing compile time errors.

    7. there are also subtle interactions between const reference and reference formal parameters and object creation

  4. overloading

    1. a function's signature is the left-to-right listing of its formal parameter types

      1. for example, the function int table(int x, table & t, string & s) has signature (int, table &, string &).

      2. formal parameter names and the return type are not part of the signature

    2. if a function is characterized by its name and its signature, then two functions can have the same name as long as they have different signatures

      1. int table(int x, table & t, string & s) { . . . }

      2. int table(int x, const table & t, string & s) { . . . }

      3. int table(int x, const table & t, string s) { . . . }

    3. two or more functions of the same name but different signatures are said to be overloaded functions

    4. operators are functions, and so can also be overloaded

      1. definition: string operator*(string & s, int m) { . . . })

      2. use: cout << string("|")*3

      3. the operators ., ::, and ?: can't be overloaded

      4. the operation's arity can't be changed

    5. C++ objects and the stl make heavy use of overloading

  5. templates

    1. creating more than a small number of overloaded functions can quickly get tedious

      1. void swap(int & i, int & j) { . . . }

      2. void swap(double & i, double & j) { . . . }

      3. void swap(string & i, string & j) { . . . }

    2. function templates avoid this problem by parameterizing functions by type

      1. template <typename T> void swap(T & i, T & j) { . . . }

    3. the C++ compiler expands each use of the function using its templated definition as a pattern

    4. each template typename must appear at least once in the function's formal parameter list

    5. template functions cannot be split into prototype and body, and they must be defined in each file they are used

    6. the STL makes heavy use of template functions


This page last modified on 31 May 2000.