Sunteți pe pagina 1din 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.

For example, there is only one definition of the array sorting problem, but many different algorithms for sorting.

Locality: When reading and reasoning about the implementation of some procedure A that calls another procedure B, we only need to look at Bs specification, not at its implementation. Modifiability: We can re implement B without changing A, as long as B still satisfies its specification. This is useful for performance tuning, or porting.
Locality helps in: Design and Decomposition Coding and Implementation Can understand the whole program one procedure at a time, rather than all at once. Much easier!

Pass-by-value-result Pass-by-value-result is an implementation model for inout-mode parameters in which actual values are moved. It is infact a combination of pass-by-value and pass-by-result. The value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable, In fact, pass-by-value-result formal parameters must have local storage associated with the called subprogram. At subprogram termination, the value of the formal parameter is transmitted back to the actual parameter.

Pass-By-Name Pass-by-name is an inout-mode parameter transmission method that does not correspond to a single implementation model, as explained below. When parameters are passed by name, the actual parameter is in effect, textually substituted for the corresponding formal parameter in all its occurrences in the subprogram. This is quite different from the methods discussed thus far.

In those cases, 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, such as between an attribute and an entity or between an operation and a symbol.

The time at which a binding takes place is called binding time. Binding and binding
times are prominent concepts in the semantics of programming languages. Bindings can take place at language design time, language implementation time, compile time, link 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. Elaboration of such a declaration refers to the storage allocation and binding process indicated by the declaration. Elaboration occurs during run time. For example, a Pascal procedure consists of a declaration section and a code section. The declaration section is elaborated just before execution of the code section begins, which happens when the procedure is called.

Storage bindings are created for variables when their declaration statements are read Advantage: allows recursion conserves storage

Disadvantages:

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. These variables, which are allocated from and deallocated to the heap, can only be referenced through pointer or reference variables. The heap is a collection of storage cells. In C++, the allocation operator, named new, 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. In fact, all their attributes are bound every time they are assigned. In a sense, they are just names that adapt to whatever use they are asked to serve. The advantage of such variables is that they have the highest degree of flexibility, allowing highly generic code to be written. The disadvantage is the run-time overhead of maintaining all the dynamic attributes, which could include array subscript types and ranges, among others

} Dynamic Scope The scope of variable in APL, SNOBOL4, and the early versions of LISP is dynamic. Dynamic scoping is based on the calling sequence of subprograms, not on their spatial relationship to each other. Thus the scope can be determined only at run time.

procedure big1 var x : integer; procedure sub1; begin .x. end; {sub1} {sub1}

procedure sub2

var x : integer; begin {sub2} end; end; {big} {sub2}

begin {big}

Static Scope Binding names to non-local variables, called static scoping. Static scoping is thus named because the scope of a variable can be statically determined that is prior to execution. In all common static scoped languages except C, C++, Java and FORTRAN, subprograms can be nested inside other subprograms.

Scope and lifetime


The scope of such a variable is from its declaration to the end reserved word of the procedure. 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.

S-ar putea să vă placă și