Comments on Exercise 1
- How it is graded?
- "Exercise 1 is OK (4 points)"
- "Exercise 1 is (almost) OK (4 points)"
- "Exercise 1 - some error (-1 or -2 or -3 or 0 points)"
- What does -1 mean?
- "Some error - please resubmit"
- Look at the comments made.
- Try not to declare variables inside a loop - there is possible overhead
- Appreciate conversions.
- Stay away from C-style casts (this will be a -1 offense in the future)
- And finally, if you are stuck or have a question, email or come early to office hours
- 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
- Writing to a file
- << operator
- Detecting EOF
What is a function?
Why write functions?
How to ...
- Function definition
- Function declaration (prototype)
- Function call
- variables inside functions
- local variables
- function arguments
Pass by value
or values that are passed to functions by value are copied in stack
memory. It is the copy that is used inside the
The original variable in the calling function is unchanged by the
Pass by Reference
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
Reference to const
A reference to a
const is used as a function argument to disallow the function from
changing the argument 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 argument is a value that is automatically passed as a function
argument when the argument is not provided by the function call.
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
Why is this an error?
funk(int arg1 = 7, int arg2);
function that is prototyped like this,
funk(int x = 2, int y = 4, int z = 6);
called in 4 different ways:
// first argument = 1, second argument = 2, third argument = 3
// first argument = 1, second argument = 2, third argument = 6
// first argument = 1, second argument = 4, third argument = 6
// first argument = 2, second argument = 4, third argument = 6