×
I'm working hard to be someone I'll be proud of.
--Your friends at LectureNotes
Close

Note for Principles of Programming Languages - ppl By Sangu Mallikharjuna reddy

  • Principles of Programming Languages - ppl
  • Note
  • 47 Views
  • Uploaded 3 months ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

UNIT III Fundamentals of Subprograms General subprogram characteristics : • • • Each subprogram has a single entry point The calling program is suspended during execution of the called subprogram Control always returns to the caller when the called subprogram’s execution terminates Basic Definitions A subprogram definition describes the interface to and the actions of the subprogram abstraction - In Python, function definitions are executable; in all other languages, they are non-executable • A subprogram call is an explicit request that the subprogram be executed • A subprogram header is the first part of the definition, including the name, the kind of subprogram, and the formal parameters • The protocol is a subprogram’s parameter profile and, if it is a function, its return type • Function declarations in C and C++ are often called prototypes parameters • • •  There are two ways that subprogram can gain access to the data that it is to process: Direct access to nonlocal variables Parameter passing ** Parameter passing is more flexible than direct access to nonlocal variables A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram An actual parameter represents a value or address used in the subprogram call statement Positional parameters: is a method for binding actual parameter to formal parameter, is done by position ** ((the first actual parameters is bound to the first formal parameter and so forth such parameters are called positional parameters )) Keyword parameters : The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter Advantage: That they can appear in any order in the actual parameter list Disadvantage: That the user of the subprogram must know the names of formal parameters int cube(int); int main(){ int y=5; cout<<cube(y); int x=3; prototype actual parameters Subprogram call

Text from page-2

int cube (int x); { subprogram header formal parameter return x*x; } } Procedures and Functions There are two distinct categories of subprograms—procedures and functions – – Procedures are collection of statements that define parameterized computations . procedures have no return values Functions structurally resemble procedures but are semantically modeled on mathematical functions. Functions have return values • procedures are expected to produce no side effects • In practice, program functions have side effec Procedures define new statements. For example, if a particular language does not have a sort statement, a user can build a procedure to sort arrays of data and use a call to that procedure in place of the unavailable sort statement. In Ada, procedures are called just that; in Fortran, they are called subroutines. Most other languages do not support procedures. Procedures can produce results in the calling program unit by two methods: (1) If there are variables that are not formal parameters but are still visible in both the procedure and the calling program unit, the procedure can change them; (2) if the procedure has formal parameters that allow the transfer of data to the caller, those parameters can be changed. procedure example as follows procedure add; var x : integer ; y : integer ; begin read ( x , y ); write ( x + y ); end Procedures has Two parts : The speification and the Body The specification is begins with the keyword PROCEDURE and ends With the procedure name or parameter list Ex : procedure a_test(a,b : in integer ; c:out Integer)

Text from page-3

– Functions define new user-defined operators. For example, if a language does not have an exponentiation operator, a function can be written that returns the value of one of its parameters raised to the power of another parameter. Its header in C++ could be float power(float base, float exp) which could be called with result = 3.4 * power(10.0, x) The standard C++ library already includes a similar function named pow. Example Void sort (int list[ ], int listlen); // function header … Sort(scores,100); // function call Design Issues for Subprograms Subprograms are complex structures in programming languages. An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment. A generic subprogram is one whose computation can be done on data of different types in different calls. • • • • • • • Are local variables static or dynamic? Can subprogram definitions appear in other subprogram definitions? What parameter passing methods are provided? Are parameter types checked? If subprograms can be passed as parameters and subprograms can be nested, what is the referencing environment of a passed subprogram? Can subprograms be overloaded? Can subprogram be generic? Local Referencing Environments Local Variables 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. local variables can be either static or stack dynamic.

Text from page-4

If local variables are stack dynamic, they are bound to storage when the subprogram begins execution and are unbound from storage when that execution terminate - ADVANTAGES OF STACK DYNAMIC LOCAL VARIABLES • • • Support for recursion Storage for locals is shared among some subprograms disadvantages of stack dynamic local variables are: – – Cost of time required to allocate, initialize and de-allocate for each activation Accesses of stack dynamic local variables must be indirect(indirect addressing), where accesses to static can be direct The subprograms cannot retain data values of Stack dynamic variables between calls. • The primary advantage of static local variables is that they are very efficient because of no indirection Example of ststic and stack dynamic variables as follows int adder(int list[], int listlen) { static int sum = 0; int count; for (count = 0; count < listlen; count ++) sum += list [count]; return sum; } In C and C++ functions, locals are stack dynamic unless specifically declared to be static. For example, in the following C (or C++) function, the variable sum is static and count is stack dynamic Nested Subprograms If a subprogram is defined within another subprogram, The idea of nesting subprograms originated with Algol 60.Ada supports nested Subprograms. Parameter passing methods  Parameter-passing methods are the ways in which parameters are transmitted to and / or from called programs Semantic Models of Parameter Passing   Formal parameters are characterized by one of three semantics models: o They can receive data from the corresponding actual parameter o They can transmit data to the actual parameter, OR o They can do both. These three semantics models are called in mode, out mode and inout mode, respectively.

Lecture Notes