You are on page 1of 5

Procedural abstraction: Give a name to a piece of code that does something; use the name to get that something

done. Example algorithm for computing sq. root}

        

C++ procedural abstractions are called functions. A function encapsulates and names an algorithm to solve a particular problem.

Increase readability is_a_factor_of(n,d) should be more meaningful than (n%d == 0) Allow reuse of code sqrt function is useful in all sorts of mathematical programs. Modular software development: Aids program design Use a function to implement subtasks of an algorithm. Allows incremental development and testing

Specification abstracts from the realization of the action, allowing multiple implementations. Parameterization abstracts from the identity of the data being used, allowing the procedure to be used in more situations. Relevant: presence, number, types of parameters Irrelevant: identity of parameters relevant: what is done Irrelevant: how it is done

Abstraction by Parameterization The packaged code can be “reused” via multiple calls. Each call binds a (different, in general) set of actual parameters to the formal parameters. When designing and coding the procedure, we write code that manipulates only the formal parameters. Thus, we do not concern ourselves with the identity of the actual parameters that are provided in a call to the procedure. Thus, we abstract from the identity of actual parameters. Abstraction by Specification Since a procedure defines a specific piece of code that has well defined entry and exit points, we can define a specification for this piece of code. A specification states what the procedure does, without stating how it does it. It is (usually) much shorter and easier to read than an implementation Thus, we abstract from the details of implementation of a procedure.

In fact. as long as B still satisfies its specification. we only need to look at B’s specification. When parameters are passed by name. At subprogram termination. the actual parameter is in effect. but many different algorithms for sorting. or porting. the value of the formal parameter is transmitted back to the actual parameter. textually substituted for the corresponding formal parameter in all its occurrences in the subprogram. which then acts as a local variable.” Much easier! Pass-by-value-result    Pass-by-value-result is an implementation model for inout-mode parameters in which actual values are moved. not at its implementation. • Modifiability: We can re implement B without changing A. Locality helps in: • Design and Decomposition • Coding and Implementation Can understand the whole program one procedure at a time. . Pass-By-Name   Pass-by-name is an inout-mode parameter transmission method that does not correspond to a single implementation model. rather than “all at once. pass-by-value-result formal parameters must have local storage associated with the called subprogram. as explained below. there is only one definition of the array sorting problem.For example. The value of the actual parameter is used to initialize the corresponding formal parameter. • Locality: When reading and reasoning about the implementation of some procedure A that calls another procedure B. This is quite different from the methods discussed thus far. This is useful for performance tuning. It is infact a combination of pass-by-value and pass-by-result.

 In those cases. Elaboration occurs during run time. such as between an attribute and an entity or between an operation and a symbol. Bindings can take place at language design time. a Pascal procedure consists of a declaration section and a code section. Elaboration of such a declaration refers to the storage allocation and binding process indicated by the declaration. For example. language implementation time. load time or run time Stack-Dynamic Variables Stack-dynamic variables are those whose storage bindings are created when their declaration statements are elaborated. compile time. • • Storage bindings are created for variables when their declaration statements are read Advantage: – allows recursion – conserves storage • Disadvantages: . formal parameters are bound to actual values or addresses at the time of the subprogram call. The Concept of Binding  A binding is an association. link time. Binding and binding times are prominent concepts in the semantics of programming languages. which happens when the procedure is called. The declaration section is elaborated just before execution of the code section begins.  The time at which a binding takes place is called binding time.

begin …. Thus the scope can be determined only at run time. among others } Dynamic Scope   The scope of variable in APL. they are just names that adapt to whatever use they are asked to serve. the allocation operator. Dynamic scoping is based on the calling sequence of subprograms. end. The advantage of such variables is that they have the highest degree of flexibility. These variables. all their attributes are bound every time they are assigned. named new. and the early versions of LISP is dynamic. In fact. which could include array subscript types and ranges. not on their spatial relationship to each other. allowing highly generic code to be written. procedure big1 var x : integer. can only be referenced through pointer or reference variables. uses a type name as its operand Implicit Heap-Dynamic Variables Implicit heap-dynamic variables are bound to heap storage only when they are assigned values. which are allocated from and deallocated to the heap. The heap is a collection of storage cells. In C++. procedure sub1.x….– Overhead of allocation and de-allocation – Subprograms cannot be history sensitive – Indirect addressing (longer time) Explicit Heap-Dynamic Variables Explicit heap-dynamic variables are nameless(abstract) memory cells that are allocated and deallocated by explicit run-time instructions specified by the programmer. The disadvantage is the run-time overhead of maintaining all the dynamic attributes. {sub1} {sub1} procedure sub2 . SNOBOL4. In a sense.

In all common static scoped languages except C. … end. The lifetime of that variable is the period of time beginning when the procedure is entered and ending when execution of the procedure reaches the end. . Java and FORTRAN. Scope and lifetime  The scope of such a variable is from its declaration to the end reserved word of the procedure. Static scoping is thus named because the scope of a variable can be statically determined that is prior to execution. begin {sub2} … end. called static scoping. C++. {big} {sub2} begin {big} Static Scope    Binding names to non-local variables.var x : integer. subprograms can be nested inside other subprograms.