FAQ | Newsboard | Telnet | Email TA | Lectures | Assignments | Man pages | Help |
---|
Remember C?
C++ is a SUPERSET of C
Functional Abstraction (functions/subprograms/subroutines/)
Separates BEHAVIOR from IMPLEMENTATION
That is:
You don't need to know how a function is IMPLEMENTED
to be able to USE it.
You only need the functional SPECIFICATION to know
HOW TO USE IT
Here's an example problem:
The problem is to write an algorithm that ensures that every open paren has
a matching closed paren.
Example:
(( 3 + 4) * 5)
Example:
if(Empty(bucket)
FillErUp();
The solution is to
Use a STACK
If ( Push If ) Pop Ignore all other charsSimple isn't it.
I have implemented a stack, and will give you an interface to it. You already know what a stack is and how it is to be used. You are working at the user level of abstraction. You do not need to know how a stack is implemented (that is my job, I am working at the stack implementer level of abstraction).
Here's a stack:
It is nice to have functions, isn't it.
Why?
Communicating with functions
Communicating by passing parameters
Pass by VALUE
Formal parameters are a COPY of the arguments
int value = 5; RetVal = Trans(value); cout << "arg: " << value; cout << " returns: " << RetVal; .... int Trans(int input) { return (input * 3 + 5); } int Trans(int input) { input = input * 3 + 5; return input; }
Pass by REFERENCE (Available in C++)
The called function can change the calling function's argument.
You use call by reference only when
you want the called function to change actual arguments
float delta;
float gamma;
initEval(delta, gamma);
cout << delta << gamma;
// only in C++
// TYPENAME & identifier
// denotes passing by reference
void initEval(float &in1, float &in2)
{
in1 = 5.0;
in2 = 10.0;
}
void initEval(float &in1, float &in2)
{
in1 = in1 * 2;
in2 += 1;
}