CIS 22B - Notes for Wed Sep 27

Announcements and Reminders

  • Check attendance sheet
  • Assignment 1 is due Monday
  • Office Hours:  Monday and Wednesday, 10:15 - 11:15 am in the ATC main computer lab

Review Topics

File I/O

  • File streams
    • ostream class
    • ofstream class
    • istream class
    • ifstream class
  • Opening a file
    • Open via constructor
    • Checking for a successful open
      • Methods: bad(), good(), fail(), the bang operator
  • Closing a file
  • Reading from a file
    • >> operator
    • get()
    • getline()
  • Writing to a file
    • << operator
    • manipulators
      • <iomanip>
    • put()
  • Detecting EOF
    Example 1
    Example 2

Functions

   What is a function?

   Why write functions?
 How to ...
  • Function definition
  • Function declaration (prototype)
  • Function call
  • variables inside functions
  • local variables
    • local static variables
  • function arguments
Pass by value
Variables or values that are passed to functions by value are copied in stack memory.  It is the copy that is used inside the function.  The original variable in the calling function is unchanged by the function.

Pass by Reference
A reference variable is an alias for another variable, defined elsewhere in the program.  Reference variables are commonly used for function arguments or return values.  The use of reference variables as function argument avoids the requirement to pass a variable's address to a function and avoid the necessity to dereference variable addresses.

Reference to const
A reference to a const is used as a function argument to disallow the function from changing the argument value.
  • Function return values
return type
return value
return by value
How do you use the function return?
Do you have to use the function return?
The answer is "no", but make sure you have a good reason not to.
  • Default Arguments
A default argument is a value that is automatically passed as a function argument when the argument is not provided by the function call.

Notes
  • Default arguments should be placed in the function prototype.  If a prototype is not provided., then the default arguments must be placed in the function heading.
  • In the function argument list, mandatory arguments must preceed default arguments.
  • Default arguments may not be specified in both the function prototype and the heading of the function definition.
  • All of a function's arguments may have default values.
  • A default value may not be applied to a reference argument
Why is this an error?
void funk(int arg1 = 7, int arg2);

A function that is prototyped like this,

void funk(int x = 2, int y = 4, int z = 6);

may be called in 4 different ways:

funk(1,2,3);     // first argument = 1, second argument = 2, third argument = 3

funk(1,2);     // first argument = 1, second argument = 2, third argument = 6

funk(1);     // first argument = 1, second argument = 4, third argument = 6

funk();     // first argument = 2, second argument = 4, third argument = 6

Videos
Read me