CIS 22B - Notes for Thursday, 9/24

Announcements and Reminders

  • Attendance using the Waiting Room - please come early
  • Exercise 1 is due NOW
  • Assignment 1 is due Tuesday
  • Virtual Club Day:  Thursday, October 1st, 12:00 - 3:00 pm
  • Last night's online session files are posted here
  • Tomorrow's TA online session is 3 pm (check Canvas for the meeting ID and password)

Comments on Exercise 1

As of 6:15 am this morning -

49 students enrolled
41 Exercise 1 received
24 students received 5 points
17 students received 3 or 4 points

Common errors (-1 point each)

Use of C-style cast
Use of C header files
Incorrect random number range
Missing comments with your name, exercise 1, compiler, and OS

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: is_open(), 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.
Do what you say you will do.
  • 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 precede 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