SUBPROGRAM

SUBPROGRAM

Subprogram is part of program that can be tested and designed independently. There are two kind of subprogram; function and procedure. Function return value, procedure just executes command.

Fundamentals of subprograms:

  • each subprogram has single entry point
  • the calling program is suspended during execution of the called subprogram
  • control always return to the caller when the called subprogram’s executions terminates

 

Local referencing environments

  • Local variables can be stack-dynamic
    • Advantages
      • Support for recursion
      • Storage for locals is shared among some subprograms
    • Disadvantages
      • Allocation/de-allocation, initialization time
      • Indirect addressing
      • Subprograms cannot be history sensitive
    • Local variables can be static
      • Advantages and disadvantages are the opposite of those for stack-dynamic local variables

Parameter-passing methods, models of parameter passing ;

Pass-by-value ( in mode ), the value of the actual parameter is used to initialize the corresponding formal parameter.

Pass-by-result ( out mode), before control is transferred back to the caller, the value of the formal parameter is transmitted back to the actual parameter. This method is sometimes called call by result. In general, pass by result technique is implemented by copy.

Pass-by-value-result / pass-by-copy (inout mode), combination of pass-by-value and pass-by-result. The formal parameters have local storage.

Pass-by-reference(aliasing) / pass-by-sharing (inout mode) , used by pass an access path. In c used by using pointer. Efficient because no copying and no duplicated storage.

Pass-by-name ( inout mode), passed by textual substitution. Formals are bound ot an access method at the time of the call, but the actual binding to a value or address takes place at the time of a reference or assignment. Allows flexibility in late binding .

 

Parameters that are subprograms,  referencing environment:

  • Shallow binding: The environment of the call statement that enacts the passed subprogram
    – Most natural for dynamic-scoped

Languages

  • Deep binding: The environment of the definition of the passed subprogram
    – Most natural for static-scoped languages
  • Ad hoc binding: The environment of the call statement that passed the subprogram

 

Calling subprograms indirectly, Usually when there are several possible subprograms to be called and the correct one on a particular run of the program is not know until execution (e.g., event handling and GUIs). In C and C++, such calls are made through function pointers.

 

Overloaded subprograms is a subprogram that has the same name with other subprogram in same refencing environment.

Generic subprograms or polymorphic subprogram takes parameter of different types on different activasions. It provide ad hoc polymorphism.

User-defined overloaded operators,

  • Operators can be overloaded in Ada, C++, Python, and Ruby
  • A Python example

def __add__ (self, second) :

                               return Complex(self.real + second.real,

self.imag + second.imag)

Use: To compute x + y, x.__add__(y)

 

Closures , 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
  • A static-scoped language that does not permit nested subprograms doesn’t need closures
  • Closures are only needed if a subprogram can access variables in nesting scopes and it can be called from anywhere
  • To support closures, an implementation may need to provide unlimited extent to some variables (because a subprogram may access a nonlocal variable that is normally no longer alive)

 

Coroutines

  • A coroutine is a subprogram that has multiple entries and controls them itself
  • Also called symmetric control: caller and called coroutines are on a more equal basis
  • A coroutine call is named a resume
  • The first resume of a coroutine is to its beginning, but subsequent calls enter at the point just after the last executed statement in the coroutine
  • Coroutines repeatedly resume each other, possibly forever
  • Coroutines provide quasi-concurrent execution of program units (the coroutines); their execution is interleaved, but not overlapped

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

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