Docsity

Procedural Abstraction and Recursion in EECS 280: Understanding Functions and Abstraction, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Procedural Abstraction and Recursion in EECS 280: Understanding Functions and Abstraction and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

EECS 280

Programming and Introductory Data Structures

Procedural Abstraction and Recursion

Slides by Brian Noble, Jeff Ringenberg and Andrew DeOrio

Abstraction

 Abstraction is a many-to-one mapping that reduces complexity and eliminates unnecessary details by providing only those details that matter.

 For example, there are several ways to implement a multiplication algorithm (table lookup, summing, etc..). Each looks quite different internally than the other, but they do the same thing. In general, a user won’t care how it’s done, just that it multiplies.

 There are two types of abstraction:  Procedural (the topic of the next three weeks)  Data

Procedural Abstraction

 For any function, there is a person who implements the function (the author) and a person who uses the function (the client).

 The author needs to think carefully about what the function is supposed to do, as well as how the function is going to do it.

 In contrast, the client only needs to consider the what , not the how. Since how is much more complicated, this is a Big Win for the client!

 In individual programming, you will often be the author and the client. Sometimes it is to your advantage to “forget the details” and only concentrate on higher levels of functionality.

Procedural Abstraction

 Procedural abstractions, done properly, have two important properties:  Local: the implementation of an abstraction can be understood without examining any other abstraction implementation.  Substitutable: you can replace one (correct) implementation of an abstraction with another (correct) one, and no callers of that abstraction will need to be modified.

 These properties only apply to implementations of abstractions, not the abstractions themselves.

 It is CRITICALLY IMPORTANT to get the abstractions right before you start writing a bunch of code.

 If you change the abstraction that is offered, the change is not local, nor is the new version substitutable.

Function Definitions vs.

Declarations

 In small programs, often we just define functions before they are used.

 In larger programs, it is useful to separate the declaration of a function from its definition. A declaration provides the “type signature” of a function, also called the function header.

 Function headers can be placed in their own file and accessed using the preprocessor directive #include.

Function Definitions vs.

Declarations Example

double GetParam(string prompt, double min, double max); //...

io.h

#include "io.h" double GetParam(string prompt, double min, double max) { /* ... */ }

io.cpp

#include "io.h" int main() { // ... GetParam(prompt, min, max); }

p1.cpp

Function Details

 The type signature of a function can be considered part of the abstraction – if you change it, customers (callers) must also change.

 However, as long as a new implementation of an abstraction does the “same thing” as some old (correct) implementation, you can replace the old one with the new one.

 Of course, now we need to know what we mean by the “same thing”. This boils down to describing the abstraction (not implementation) of the function. We use specifications to do this.

Specifications

 For a procedural abstraction, a specification must answer three questions:  What pre-conditions must hold to use the function?  Does the function change any inputs (even implicit ones)? If so, how?  What does the procedure actually do?

 We answer each of these three questions in a specification comment, and we always include one – with the declaration, if it is separate from definition.

Specification Comment Example

bool isEven(int n); // EFFECTS: returns true if n is even,

//false otherwise

 This function returns true if and only if its argument is an even number. We call functions that return true or false depending on some input property predicates.

 Since the predicate isEven is well-defined over all inputs (every possible integer is either even or odd) there need be no REQUIRES clause.

 Since isEven modifies no (implicit or explicit) arguments,

there need be no MODIFIES clause.

Specification Comment Example

int factorial(int n); // REQUIRES: n >= 0 // EFFECTS: returns n!

 The mathematical abstraction factorial is only defined for non- negative integers. So, we REQUIRE that the caller supply an argument greater than or equal to zero.

 Factorial promises to compute n! correctly for non-negative integers only. In other words, the EFFECTS clause is only valid for inputs satisfying the REQUIRES clause.

 Importantly, this means that the implementation of factorial DOES NOT HAVE TO CHECK if n < 0! The function specification tells the caller that s/he must pass a non-negative integer.

More Function Details

 What about the MODIFIES clause?

 A MODIFIES clause identifies any function argument or piece of global state that might change if this function is called.  For example, this can happen with pass-by-reference as opposed to pass- by-value inputs

Specification Comment Example

void swap(int &x, int &y); // MODIFIES: x, y // EFFECTS: exchanges the values of x and y

 The ampersand (&) means that you get a reference to the caller's argument, not a copy of it. This lets you to change the value of that argument.

 NOTE: If the function could change a reference argument, it must go in the MODIFIES clause. Leave it out only if the function can never change it.  This implies you should never use pass-by reference, unless you intend to change the input in some situation.

Call by reference example

int main()

a:

b:

void swap(int &x, int &y)

x:

y:

Call Stacks: How function calls work

 When we call a function (using pass-by-value semantics) the program follows these steps:

  1. Evaluate the actual arguments to the function (order is not guaranteed).
  2. Create an “activation record” (sometimes called a “stack frame”) to hold the function's formal arguments and local variables.
  3. Copy the actuals' values to the formals' storage space.
  4. Evaluate the function in its local scope.
  5. Replace the function call with the result.
  6. Destroy the activation record.