1 / 4

Procedure Definitions and Semantics

This article explores the concepts of procedure definitions and semantics in programming languages. It discusses the components of a procedure, the separation of declaration and invocation, and the execution of procedure bodies. The article also covers the semantics of procedures, including block structure, activation records, parameters, nonlocal references, closures, and different parameter passing methods.

lnatasha
Download Presentation

Procedure Definitions and Semantics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Procedure Definitions and Semantics • Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined by two elements: • A specification (protocol, interface, declaration) • A body (definition) • The specification usually identifies the parameters (Perl uses a default parameter list @_) and usually declares their types (not in weakly typed languages). • In some cases it is critical to be able to separate a procedure’s declaration from its invocation. This happens, for example, when one must construct mutually recursive procedures. • The body usually defines a block of code that is to be executed when the procedure is called (invoked, activated, executed). • A procedure is called by specifying its name and a list of arguments (actual parameters) to be bound to the (formal) parameters of the procedure. The procedure body is then executed. • In the old-old days, functions returned values and procedures didn’t. It is common nowadays to blur the line of distinction between these and let any procedure return a value. • Control returns from a procedure when its execution is completed either by leaving its block or by executing a return statement. The value returned can either be implicit (the value returned by the last executed statement in the block) or explicit (the value associate with the executed return statement.

  2. Procedure Semantics • Most current language implementations follow the tenets of block structure in procedure call semantics. • Each procedure definition is a block. • When a procedure is invoked, an activation record (frame) is created to contain name bindings for that procedure during its lifetime. This activation record contains two critical references: • The dynamic chain pointer (this points to the caller’s activation record in the stack. This is remembered so that the caller’s activation can be restored upon return) • The static chain pointer (this points to the most recent activation record of the block in which this procedure was declared. This is remembered so that references to non-local variables can be found from the lexical (or static) nesting structure of the program.

  3. Parameters Nonlocals and Closures • Procedures are useless unless they can compute different values in different contexts. To do this requires information from outside the procedure body. • Two methods exist to get such external values: • Parameters • Nonlocal references • If a procedure has no nonlocal references (if it uses only its parameters) it is said to be closed. • In languages that can create procedures at runtime, the nonlocal referencing environment of a procedure must be stored along with it at runtime. The object that represents a procedure together with its nonlocal referencing environment is known as a closure.

  4. Parameter Passing Methods • Pass by ValueThis method is common and useful. The values of the arguments are copied into the activation record of the called procedure. As C demonstrates, aliases can occur if pointers are passed by value. • Pass by ReferenceThis method allocates a hidden or buried pointer to the argument in the called procedure’s activation record. An implicit dereference is performed each time the parameter is used. This allows the call to be made without copying the argument (which could be a large structure or array).Problems can occur if expressions are allowed to be passed by reference. • Pass by Value-ResultThis method is quite rare. It involves pass by value (copy-in), followed by execution of the body, followed by a copy-out of the parameter values to the original arguments. • Pass by NameThis method (from Algol 60) was created when parameter passing was poorly understood. It involves implementing the same behavior as would occur if the arguments were textually substituted (with appropriate renamings) in place of the parameters in the body, and the body were then evaluated. The concept of a thunk (a piece of code to evaluate the name parameter) was invented to implement this complex method. The complexity is clear when one realizes it’s impossible to write a swap procedure using name parameter passing and also looks at Jensen’s device.

More Related