+ All Categories
Home > Documents > Building Abstractions with Procedures (Part 1)

Building Abstractions with Procedures (Part 1)

Date post: 11-Feb-2016
Category:
Upload: genera
View: 54 times
Download: 0 times
Share this document with a friend
Description:
Building Abstractions with Procedures (Part 1). CS 21a: Introduction to Computing I First Semester, 2013-2014. Last Time…. The basic building blocks of programming and basic ways of combining them Expressions and Statements The basic means of abstraction Variables. Today…. - PowerPoint PPT Presentation
Popular Tags:
51
Building Abstractions with Procedures (Part 1) CS 21a: Introduction to Computing I First Semester, 2013-2014
Transcript
Page 1: Building Abstractions with Procedures  (Part 1)

Building Abstractions with Procedures (Part

1)CS 21a: Introduction to

Computing IFirst Semester, 2013-2014

Page 2: Building Abstractions with Procedures  (Part 1)

Last Time…►The basic building blocks of

programming and basic ways of combining them►Expressions and Statements

►The basic means of abstraction►Variables

Page 3: Building Abstractions with Procedures  (Part 1)

Today…►A first step in making systems

modular instead of monolithic

Page 4: Building Abstractions with Procedures  (Part 1)

Outline►Abstraction through Procedures►Procedure Calls and Environments►Procedural Programming In-Depth►Design by Contract

Page 5: Building Abstractions with Procedures  (Part 1)

Consider the Following Program Fragment…

int banana_cost = number_of_bananas * cost_per_banana;int coconut_cost = number_of_coconuts * cost_per_coconut;int apple_cost = number_of_apples * cost_per_apple;int total_cost = banana_cost + coconut_cost + apple_cost;

Page 6: Building Abstractions with Procedures  (Part 1)

Some Observations…►Each fruit cost has been abstracted

away.►The statement for the total does not

need to concern itself with quantities and unit costs.

►Later, we’ll see how iteration and recursion can expand this to arbitrarily many fruits.

►Similarity of computing for banana_cost, coconut_cost, apple_cost

Page 7: Building Abstractions with Procedures  (Part 1)

Abstraction through Procedures

►The common process involved can be given a name.

►Details can be hidden by just saying that the cost is a function of quantity and unit price.

►This computational idea can be embodied in a procedure.

static int cost(int quantity, int unit_price) {

return quantity * unit price;

}

parameters

The return statement is another kind of statement.

proceduredefinition

Page 8: Building Abstractions with Procedures  (Part 1)

The Earlier Program Can Now Be Written As…

int banana_cost = cost(number_of_bananas, cost_per_banana);

int coconut_cost = cost(number_of_coconuts, cost_per_coconut);

int apple_cost = cost(number_of_apples, cost_per_apple);

int total_cost = banana_cost + coconut_cost + apple_cost;

Page 9: Building Abstractions with Procedures  (Part 1)

Another Examplestatic int square(int x){

return x*x;}public static void main(String args[]){

print(square(969));}

Page 10: Building Abstractions with Procedures  (Part 1)

Is It Worth the Trouble?►A general rule has been discovered and

written down. +1 for science.►The above example was simple, but if the

procedure were more complicated…►Code becomes shorter and easier for humans

to understand and maintain. +1 for humans.►Abstraction and modularity yields another

advantage: reusability. Bonus +1 for engineers.

Page 11: Building Abstractions with Procedures  (Part 1)

Is It Worth the Trouble?►The true power of this is that

procedure definitions can have procedure calls within them, which we’ll get to later.

Page 12: Building Abstractions with Procedures  (Part 1)

Anatomy of a Procedure Call

int coconut_cost = cost(number_of_coconuts, cost_per_coconut);

arguments

procedure call

Page 13: Building Abstractions with Procedures  (Part 1)

Pass by Value►We say…►The value of number_of_coconuts is

passed as an argument to the procedure call cost for the parameter quantity.

►The value of cost_per_coconut is passed as an argument to the procedure call cost for the parameter unit_price.

Page 14: Building Abstractions with Procedures  (Part 1)

Pass by Value►If some complicated expression were

passed instead, the expression is evaluated first and the procedure call cares only about the resulting value.

int foo = 2, bar = 15, deadbeef = 5;cost(2*3+4+6/foo, 12+bar/deadbeef-3);becomes, during the computation,cost(13, 12);before the procedure call proceeds.

This is called applicative-order evaluation. But you don’t have to know that now.

Page 15: Building Abstractions with Procedures  (Part 1)

Practice►Define the cost procedure as above.

► Outside main, but inside the class►Call the cost procedure within the

definition of main to calculate different costs.► Try to pass numbers as arguments.► Try to pass variables as arguments.► Try to pass expressions (with or without

variables) as arguments.

Page 16: Building Abstractions with Procedures  (Part 1)

Outline►Abstraction through Procedures►Procedure Calls and

Environments►Procedural Programming In-Depth►Design by Contract

Page 17: Building Abstractions with Procedures  (Part 1)

Different Procedure Calls Produce Different Environments

►Each time cost was called…►A new table of name-value pairs was created.► quantity had a new, possibly different from

before, value.► unit_price had a new, possibly different

from before, value.►Each procedure call is executed in a

context or environment independent from each other.

Page 18: Building Abstractions with Procedures  (Part 1)

Different Procedure Calls Produce Different Environments

Id Value

number_of_bananas 12cost_per_banana 3number_of_coconuts 5cost_per_coconut 15

main

int number_of_bananas = 12, cost_per_banana = 3,number_of_coconuts = 5,cost_per_coconut = 15;

Page 19: Building Abstractions with Procedures  (Part 1)

Different Procedure Calls Produce Different Environments

maincost

Id Value

quantity 12unit_price 3

int banana_cost = cost(number_of_bananas, cost_per_banana);

Page 20: Building Abstractions with Procedures  (Part 1)

Different Procedure Calls Produce Different Environments

Id Value

number_of_bananas 12cost_per_banana 3number_of_coconuts 5cost_per_coconut 15banana_cost 36

main

int banana_cost = cost(number_of_bananas, cost_per_banana);

Page 21: Building Abstractions with Procedures  (Part 1)

Different Procedure Calls Produce Different Environments

maincost

Id Value

quantity 5unit_price 15

int coconut_cost = cost(number_of_coconuts, cost_per_coconut);

Page 22: Building Abstractions with Procedures  (Part 1)

Different Procedure Calls Produce Different Environments

Id Value

number_of_bananas 12cost_per_banana 3number_of_coconuts 5cost_per_coconut 15banana_cost 36coconut_cost 75

main

int coconut_cost = cost(number_of_coconuts, cost_per_coconut);

Page 23: Building Abstractions with Procedures  (Part 1)

Different Procedure Calls Produce Different Environments

► The collection of procedure calls in a computation (not in a program, not the written calls but the running calls) is called the procedure or method stack.

► Each layer in the method stack has an environment associated with it.

► When a procedure is called a new environment is created, and a new stack frame is pushed on to the method stack.

► When the return statement of a procedure call is executed, the environment is destroyed, and the top stack frame is popped from the method stack.

Page 24: Building Abstractions with Procedures  (Part 1)

Outline►Abstraction through Procedures►Procedure Calls and Environments►Procedural Programming In-

Depth►Design by Contract

Page 25: Building Abstractions with Procedures  (Part 1)

Monolithic versus Modular

Page 26: Building Abstractions with Procedures  (Part 1)

Monolithic versus Modular►Non-modular imperative

► “Do this, then do this, …”► All in one big chunk

►Modular imperative (also called procedural)► “Do this chunk, then do this chunk, …”► I’ll tell you how to break down the chunks

into smaller chunks elsewhere.► Allows creation of layers of abstraction

Page 27: Building Abstractions with Procedures  (Part 1)

Without Procedures, Imperative Programs Look

Like This

Open HandPut Hand Next to Spoon

Close HandMove Hand Till Spoon Touches

Food

Page 28: Building Abstractions with Procedures  (Part 1)

With Procedures, Imperative Programs Look Like This

EatPut Food in Mouth

Grab Spoon

Open

Hand

Put Hand Next

to Spoon

Close

Hand

Scoop

Food

Put Spoon in

Mouth

Take Spoon Out,

Leaving Food

In

ChewMov

e Jaw Up

Move

Jaw Dow

n

Repeat

Swallow

Page 29: Building Abstractions with Procedures  (Part 1)

Take Note►The procedure definition is not the same

as the procedure call.►The procedure definition belongs to the

program.►The procedure call is written in the

program.►BUT each procedure call is like a request

to do a computation.The written procedure call is part of the procedure definition (defining main), which is part of the program. The running procedure call is part of the computation.

Page 30: Building Abstractions with Procedures  (Part 1)

Take Note►Parameters are not the same as arguments.

► Parameters are variables. Arguments are values.► Even if you pass a variable as an argument, the

variable has to be evaluated first before it is received by the procedure call.►As far as cost is concerned, there is no coconut_cost or cost_per_coconut or foo or bar or deadbeef. There is only the value for quantity and the value for unit_price.

►Meaning you can also pass expressions as arguments

Page 31: Building Abstractions with Procedures  (Part 1)

Take Note►The return statement is not the same

as the return value.

Page 32: Building Abstractions with Procedures  (Part 1)

Procedures Look Like Functions, But…

► Is a valid mathematical function►But in no way says how to actually get y

►It does suggest how to do it: try all possible y's, but that's not really possible to do with real numbers

Page 33: Building Abstractions with Procedures  (Part 1)

Mathematical Functions Versus Algorithmic Procedures

►Functions are what-is definitions.►Procedures are how-to definitions.►Sometimes the translation is trivial (square),

sometimes not (square root) or there is a better way to compute it than suggested by the mathematical definition.►Or sometimes, there is no better way to do it

(hard problems).►The functional programming paradigm

attempts to unify the two.

Page 34: Building Abstractions with Procedures  (Part 1)

Is Procedural Same As Functional?

►Procedural is a special kind of imperative.

►Functional is altogether different.►Because it expresses how-to definition

in terms of what-is definitions, it is a special kind of declarative programming.

►“This is, and this is,…” Oh, by the way, the computer can run it.

Page 35: Building Abstractions with Procedures  (Part 1)

Question►Why is it possible to

square(4);►But not to

4 * 4;?

Page 36: Building Abstractions with Procedures  (Part 1)

Question►Why is it possible to

square(4);►But not to

4 * 4;?

Procedure calls are statements, but expressions are not. Expressions cannot be executed, only evaluated. This is not true of programming in general, but is an artefact of the imperative programming paradigm.

In the first statement, the return value is simply ignored and can’t be used further in a computation because the algorithm contains no variable to refer to it.

Page 37: Building Abstractions with Procedures  (Part 1)

Why Two Paradigms?►Imperative is closer to computer

architecture, and is more powerful in terms of performance (better for computers).

►Declarative is closer to mathematics, and is more powerful in terms of elegance and conciseness (better for science).

►The split began in the 1950s, but the two are slowly merging today.

Unfortunately, you will not learn the declarative styles of programming in this class.

Page 38: Building Abstractions with Procedures  (Part 1)

Recommended Reading►“Revenge of the Nerds”, by Paul

Graham. ►But come back to this again after

learning programming.►Don’t worry if you don’t understand

everything here.

Page 39: Building Abstractions with Procedures  (Part 1)

Practice Programming Problem: Interest Again

►Rewrite the program you wrote for Alice so that it can potentially be used by Bob, who works as a financial adviser for several people and who might want to get several answers, not just using the same program, but in the same computation.►Simply, just make the program cleaner.

►What procedures could you use here?

Page 40: Building Abstractions with Procedures  (Part 1)

Note►At this point, our program doesn’t really allow Bob

to do what he wants.► Later, we will see how to extend this program…

1. So that we can actually send Bob a ready-to-run program.► Receive any (but only one) input case and to give an

answer for it with the same program (without changing the variable declarations and recompiling)

2. So that Bob can actually do a batch computation.► Receive an arbitrary number of input cases and to give

an answer for each of them, not just with the same program, but also in the same computation

Page 41: Building Abstractions with Procedures  (Part 1)

Outline►Abstraction through Procedures►Procedure Calls and Environments►Procedural Programming In-Depth►Design by Contract

Page 42: Building Abstractions with Procedures  (Part 1)

Use Versus Definition►When you do this kind of abstraction, you are

fulfilling two roles► The programmer working on the larger system who

uses the procedure but exercises blackboxing (don’t care what goes on inside, as long as it gives the correct output for my correct input).

► The programmer whose responsibility it is to make the black box work.

► If you collaborate with other people, you may be required to fulfill only one of the roles at a time.

Writing a procedure is like writing a new, smaller program embedded in the larger one. The larger algorithm delegates a part of the problem, or sub-problem to the procedure.

Page 43: Building Abstractions with Procedures  (Part 1)

Wait What, There’s a Correct Input?► Part of the contract between the user and the

implementer is that the user passes only the appropriate input values to the procedure.► The user has to call the procedure with the correct

arguments, so that the procedure’s parameters have well-defined values.

► All programs operate on the assumption that the input follows a specified format.► It is the user’s responsibility to not give invalid input.►But it is the implementer’s responsibility to handle

unusual input, or special cases.

Page 44: Building Abstractions with Procedures  (Part 1)

This Way, Programs Can Be Pipelined One After the Other

►Like the direct sequencing of statements

►Each statement/expression/procedure definition can be viewed as a mini-program.

►Each statement execution/expression evaluation/procedure call can be viewed as mini-computation.

Page 45: Building Abstractions with Procedures  (Part 1)

Design by Contract►Each module (procedure in this case)

expects valid input, and is expected to return the correct output.►Each module is responsible for correctly

solving a sub-problem.►Modules all have to follow their

contracts, so that when they are tied together, they correctly solve the whole problem.

Page 46: Building Abstractions with Procedures  (Part 1)

Note the Analogies► Procedure Definition► Procedure► (Running)Procedure Call

► Parameter► Argument► Return Value

► Program► Algorithm► Computation

► Part of Algorithm► Part of Computation► Part of Computation

• Problem• Problem

Instance

• Input Specs• Input• Output

Page 47: Building Abstractions with Procedures  (Part 1)

Note the Analogies► Parameter Identifier► Parameter► Argument

► Because really, a parameter is just a kind of variable

► Variable Identifier► Variable► Value

Page 48: Building Abstractions with Procedures  (Part 1)

Summary►Common patterns lead to abstraction.► Procedural abstraction is next step after

variables. ►Arguments are assigned to the parameters of a

procedure call when values are passed to it (and only values are passed) .

►A procedure call can return a value when the procedure definition contains a return statement.

►Running procedure calls can be visualized with a method stack.

Page 49: Building Abstractions with Procedures  (Part 1)

Summary►Functions are different from

procedures.►Declarative (which includes

functional) and imperative (which includes procedural) are the two main styles of programming.

Page 50: Building Abstractions with Procedures  (Part 1)

Summary►Definition is not use.►Procedures are defined, then called.

The two are not the same.►Procedural abstraction leads to

blackboxing, which can only work through contracts between users and implementers.

Page 51: Building Abstractions with Procedures  (Part 1)

Next Time…►More Combinations through

Procedures►Walls can be built with bricks, and cities

can be built with walls.►Procedural Programming In-Depth


Recommended