PLC Session 7

In this seventh session we discussed about Subprogram. This actually should be our group’s turn to present the material. But due to the time was before mid term test our group didn’t have the chance to give presentation, so our group presentation delayed for the sake of mid term semester test. Okay let’s talk about subprogram, this session divided into bunch of topics that shown below

  • Introduction
  • Fundamentals of Subprograms
  • Local Referencing Environments
  • Parameter-Passing Methods
  • Parameters That Are Subprograms
  • Calling Subprograms Indirectly
  • Overloaded Subprograms
  • Generic Subprograms
  • User-Defined Overloaded Operators
  • Closures
  • Coroutines

Without further ado I will discuss the first topic which is introduction.Two fundamental abstraction facilities can be included in a programming language : process abstraction and data abstraction. In the early history of high level programming languages, only process abstraction was included. Process abstraction, in the form of subprograms, has been a central concept in all programming languages. In the 1980’s, however, many people began to believe that data abstraction was equally important. Next topic is about Fundamentals of Subprograms. Subprogram have the following characteristics. Each subprogram has a single entry point. The calling program unit is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time. And the third is, control always returns to the caller when the subprogram execution terminates.A subprogram definition describes the interface to and the actions of the subprogram abstraction. A subprogram call is the explicit request that a specific subprogram be executed. A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution. A subprogram header, which is the first part of the definition, serves several purposes. First, it specifies that the following syntactic unit is a subprogram definition of some particular kind. Second, if the subprogram is not anonymous, the header provides a name for the subprogram. Third, it may optionally specify a list of parameters.In C, the header of a function named adder might be as shown below:

void adder (parameters)

The reserved word void in this header indicates that the subprogram does not return a value. We also learned that Subprograms can have declarations as well as definitions. This form parallels the variable declarations and definitions in C, in which the declarations can be used to provide type information but not to define variables. Subprogram declarations provide the subprogram’s protocol but do not include their bodies. Function declarations are common in C and C++ programs, where they are called prototypes. Such declarations are often placed in header files.In most other languages (other than C and C++), subprograms do not need declarations, because there is no requirement that subprograms be defined before they are called.There are two ways that a nonmethod subprogram can gain access to the data that it is to process: through direct access to nonlocal variables or through parameter passing. Parameter passing is more flexible than direct access to nonlocal variables. In essence, a subprogram with parameter access to the data that it is to process is a parameterized computation. It can perform its computation on whatever data it receives through its parameters. Let’s move on to the next topic about Local Referencing Environments.

In this topic we learned that Subprograms can define their own variables, thereby defining local referencing environments. Variables that are defined inside subprograms are called local variables, because their scope is usually the body of the subprogram in which they are defined. In most contemporary languages, local variables in a subprogram are by default stack dynamic. In C and C++ functions, locals are stack dynamic unless specifically declared to be static. The next topic is about Parameter-Passing Methods , Parameter-passing methods are the ways in which parameters are transmitted to and/or from called subprograms formal parameters are characterized by one of three distinct semantics models: (1) They can receive data from the corresponding actual parameter; (2) they can transmit data to the actual parameter; or (3) they can do both. These models are called in mode, out mode, and inout mode, respectively. There are ways to do paramater passing we will discuss some of them here. When a parameter is passed by value, the value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local variable in the subprogram, thus implementing in-mode semantics. Pass-by-value is normally implemented by copy, because accesses often are more efficient with this approach. It could be implemented by transmitting an access path to the value of the actual parameter in the caller, but that would require that the value be in a write-protected cell. We learned that The advantage of pass-by-value is that for scalars it is fast, in both linkage cost and access time. The main disadvantage of the pass-by-value method if copies are used is that additional storage is required for the formal parameter, either in the called subprogram or in some area outside both the caller and the called subprogram.

Pass-by-result is an implementation model for out-mode parameters. When a parameter is passed by result, no value is transmitted to the subprogram. The corresponding formal parameter acts as a local variable, but just before control is transferred back to the caller, its value is transmitted back to the caller’s actual parameter, which obviously must be a variable. Pass-by-reference is an  implementation model for inout-mode parameters. Rather than copying data values back and forth, however, as in pass-by value-result, the pass-by-reference method transmits an access path, usually just an address, to the called subprogram. This provides the access path to the cell storing the actual parameter. Thus, the called subprogram is allowed to access the actual parameter in the calling program unit. In effect, the actual parameter is shared with the called subprogram. There are actually some other way to pass parameter but we only covers the three above for the sake of simplicity. Next topic is about Parameters That Are Subprograms.

There are three choices:
• The environment of the call statement that enacts the passed subprogram
(shallow binding)
• The environment of the definition of the passed subprogram (deep
binding)
• The environment of the call statement that passed the subprogram as an
actual parameter (ad hoc binding)

Move on to next topic is Calling Subprograms Indirectly. There are situations in which subprograms must be called indirectly. These most often occur when the specific subprogram to be called is not known until run time. The call to the subprogram is made through a pointer or reference to the subprogram, which has been set during execution before the call is made. The two most common applications of indirect subprogram calls are for event handling in graphical user interfaces, which are now part of nearly all Web applications, as well as many non-Web applications, and for callbacks, in which a subprogram is called and instructed to notify the caller when the called subprogram has completed its work. It is Overloaded Subprograms as the next topic, An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. Every version of an overloaded subprogram must have a unique protocol. Next topic is about Generic Subprograms, we learned A polymorphic subprogram takes parameters of different types on different activations. Overloaded subprograms provide a particular kind of polymorphism called ad hoc polymorphism. Overloaded subprograms need not behave similarly. Move on to the next topic there is User-Defined Overloaded Operators, Operators can be overloaded by the user in Ada, C++, Python, and Ruby. Suppose that a Python class is developed to support complex numbers and arithmetic operations on them. Next topic is about Closures, a closure is a subprogram and the referencing environment where it was defined. The referencing environment is needed if the subprogram can be called from any arbitrary place in the program. Last but not least  there are Coroutines A coroutine is a special kind of subprogram. Rather than the master-slave relationship between a caller and a called subprogram that exists with conventional subprograms, caller and called coroutines are more equitable. In fact, the coroutine control mechanism is often called the symmetric unit control model. Coroutines can have multiple entry points, which are controlled by the coroutines themselves.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *