CS202 Computer Science II
FAQ Newsboard  Telnet Email TA Lectures Assignments Man pages Help

Read Chapter 1


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 chars

Simple isn't it.
If there is a problem in Popping --> mismatched paren
Otherwise matched parens

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:

  1. void InitStack(void)
  2. boolean Empty(void)
  3. boolean Full(void)
  4. void Push(char ch)
  5. char Pop(void)
  6. void StackPrint(void)

Functional abstraction is nice, it insulates us from the implementation.

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; }

Trans' copy may change but the calling function's copy does not


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;
}



DOCUMENTATION CONVENTIONS