Presentation is loading. Please wait.

Presentation is loading. Please wait.

ISBN 0-321-49362-1 Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.

Similar presentations


Presentation on theme: "ISBN 0-321-49362-1 Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms."— Presentation transcript:

1 ISBN 0-321-49362-1 Chapter 9 Subprograms

2 Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprograms Overloaded Subprograms Generic Subprograms Design Issues for Functions User-Defined Overloaded Operators Coroutines

3 Copyright © 2007 Addison-Wesley. All rights reserved.1-3 Introduction Two fundamental abstraction facilities –Process abstraction Emphasized from early days –Data abstraction Emphasized in the1980s

4 Copyright © 2007 Addison-Wesley. All rights reserved.1-4 Fundamentals of Subprograms 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

5 Copyright © 2007 Addison-Wesley. All rights reserved.1-5 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. For example: if … def fun(…): else def fun(…): … 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. For example: –Subroutine Adder ( parameters ) Fortran –Procedure Adder (parameters) Ada –def adder ( parameters ): Python –void adder ( parameters) C The parameter profile (aka signature) of a subprogram is the number, order, and types of its parameters. The protocol is a subprogram’s parameter profile and, if it is a function, its return type.

6 Copyright © 2007 Addison-Wesley. All rights reserved.1-6 Basic Definitions (continued) Function declarations in C and C++ are often called prototypes A subprogram declaration provides the protocol, but not the body, of the subprogram 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

7 Copyright © 2007 Addison-Wesley. All rights reserved.1-7 Actual/Formal Parameter Correspondence Positional –The binding of actual parameters to formal parameters is by position: the first actual parameter is bound to the first formal parameter and so forth –Safe and effective Keyword –The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter –Example call to a Python function: Sumer( length = my_length, list = my_array, sum = my_sum ) –Advantage: Parameters can appear in any order, thereby avoiding parameter correspondence errors –Disadvantage: User must know the formal parameter’s names Both Positional and Keyword –Ada, Fortran95, and Python –Example: Sumer( my_length, sum = my_sum, list = my_array )

8 Copyright © 2007 Addison-Wesley. All rights reserved.1-8 Formal Parameter Default Values In some languages, C++, Python, Ruby, Ada, and PHP, formal parameters can be assigned a default values, if the actual parameter is not passed. Python function example: def compute_pay ( income, exemptions = 1, tax_rate ) Function call example: pay = compute_pay(20000.0, tax_rate = 0.15) –In C++, default parameters must appear last because parameters are positionally associated. For example: Float compute_pay ( float income, float tax_rate, int exemptions = 1) –To call : pay = compute_pay(20000.0, 0.15)

9 Copyright © 2007 Addison-Wesley. All rights reserved.1-9 Formal Parameter Default Values Variable numbers of parameters –C, C++, Perl and JavaScript allow the number of actual parameters to be fewer than the number of formal parameters. For example: printf function of C –C# allows a variable number of parameters as long as they are of the same type. For example: public void DisplayList ( params int [] list ) { foreach (int next in list ) { Console.WriteLine(“Next value {0}”, next); } Call examples: myObject.DisplayList(myList);// myList is an int array. myObject.DisplayList(2, 4, 3 * x – 1, 17); - Ruby and Python also support variable numbers of parameters. In Ruby, the actual parameters are sent as elements of a hash literal and the corresponding formal parameter is preceded by an asterisk. - In Python, the actual is a list of values and the corresponding formal parameter is a name with an asterisk

10 Copyright © 2007 Addison-Wesley. All rights reserved.1-10 Procedures and Functions There are two categories of subprograms –Procedures are collection of statements that define parameterized computations –Functions structurally resemble procedures but are semantically modeled on mathematical functions They are expected to produce no side effects In practice, program functions have side effects

11 Copyright © 2007 Addison-Wesley. All rights reserved.1-11 Design Issues for Subprograms 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?

12 Copyright © 2007 Addison-Wesley. All rights reserved.1-12 Local Variables Are local variables static or dynamic? Local variables can be stack-dynamic - Advantages Support for recursion Storage for locals is shared among some subprograms –Disadvantages Allocation/de-allocation, initialization time Uses 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

13 Copyright © 2007 Addison-Wesley. All rights reserved.1-13 Local Variables Most languages implement local variables as stack-dynamic variables. Ada and methods of C++, C# and Java. Fortran95 allows the user to choose via the Save statement. In this PL/I example, Count is stack-dynamic due to the fact that the subroutine is marked as Recursive, whereas, Sum is so due to the keyword Save. Recursive Subroutine Sub() Integer :: Count Save, Real :: Sum … End Subroutine Sub

14 Copyright © 2007 Addison-Wesley. All rights reserved.1-14 Local Variables The default for local variables in C/C++ functions is stack- dynamic, however, by using the keyword static in the declaration, they can be implemented as static variables. –Example: int adder(int list[], int listlen) { static int sum = 0; int count; for (count = 0; count < listlen; count++) sum += list[count]; return sum; } Non-static version: int adder(int list[], int listlen) { int sum = 0; int count; for (count = 0; count < listlen; count++) sum += list[count]; return sum; }

15 Copyright © 2007 Addison-Wesley. All rights reserved.1-15 Nested Subprograms This idea originated with Algol 60. Motivation was to create a hierarchy of both logic and scope. Combined with static scoping, this provides a highly structured way to grant access to nonlocal variables in enclosing subprograms. Allowed by Algol60, Algol 68, Pascal and Ada. Not allowed by C, C++ and Java. Allowed by JavaScript, Python and Ruby.

16 Copyright © 2007 Addison-Wesley. All rights reserved.1-16 Parameter Passing Semantic Models of Parameter Passing –In mode –Out mode –Inout mode Conceptual Models of Transfer –An actual value is copied to the caller, to the callee, or both ways (see above) –An access path is transmitted, such as a simple pointer or reference.

17 Three Semantic Models of Parameter Passing When Values are Copied.

18 Copyright © 2007 Addison-Wesley. All rights reserved.1-18 Models of Parameter Passing

19 Copyright © 2007 Addison-Wesley. All rights reserved.1-19 Implementation Model of Parameter Passing Implementation Models of Parameter Passing –Pass-by-value –Pass-by-result –Pass-by-value–result –Pass-by-reference –Pass-by-name

20 Copyright © 2007 Addison-Wesley. All rights reserved.1-20 Pass-by-Value (In Mode) The value of the actual parameter is used to initialize the corresponding formal parameter, which then acts as a local. –Normally implemented by copying. Disadvantages: (if by physical move): additional storage is required (stored twice) and the actual move can be costly (for large parameters). –Can be implemented by transmitting an access path. Disadvantages: must write-protect the variable in the called subprogram and accesses cost more (indirect addressing).

21 Copyright © 2007 Addison-Wesley. All rights reserved.1-21 Pass-by-Result (Out Mode) No value is transmitted to the subprogram. The corresponding formal parameter acts as a local variable, with its value transmitted to caller’s actual parameter when control is returned to the caller, by a physical move. Same advantages/disadvantages of pass-by-value. If values are returned by access path, then there is the problem of ensuring that the initial value of the actual paramete ris not used in the called subprogram. The difficulties of this lead to values returned by copy method. If values are returned by copy, then disadvantages are: –Extra storage and overhead of the copy operation. –Actual parameter collision. –Time at which the return address is evaluated.

22 Copyright © 2007 Addison-Wesley. All rights reserved.1-22 Pass-by-Result (Out Mode) Actual parameter collision. Consider the call: sub(p1, p1); whichever formal parameter is copied to its corresponding actual parameter last will represent the current value of p1 in the caller. Example in C#. void Fixer (out int x, out int y) { s = 17; y = 35; } … f.Fixer(out a, out a); What is the value of a upon return from the function call. Depends upon the implementation. Which of the copies from formal to actual occurs last. Since this order may vary by different implementations, the results of the same program may also vary.

23 1-23 Pass-by-Result (Out Mode) Problem of the implementation’s choice of time at which the return address is evaluated. –Time of the call –Time of the return. –Example in C#. void DoIt( out int x, int index) { x = 17; index = 42; }.. sub = 21; f.DoIt(list[sub], sub); –Result if address of list[sub] is evaluated at the time of the call: Value of 17 will be returned to list[21] –Result if address of list[sub] is evaluated at the time of the return: Value of 17 will be returned to list[42]; –Again, since implementations may vary, the results of the same program may also vary.

24 Copyright © 2007 Addison-Wesley. All rights reserved.1-24 Pass-by-Value-Result (inout Mode) A combination of pass-by-value and pass-by-result. Sometimes called pass-by-copy. Formal parameters have local storage Disadvantages: –Those of pass-by-result –Those of pass-by-value Advantages are relative to pass-by- reference.

25 Copyright © 2007 Addison-Wesley. All rights reserved.1-25 Pass-by-Reference (Inout Mode) Passes an access path. Also called pass-by-sharing Advantage: –Passing process is efficient –No duplicated storage Disadvantages –Slower accesses (compared to pass-by-value) to formal parameters –Potentials for unwanted changes to the actual parameter. –Unwanted aliases (access broadened)

26 Pass-by-Reference (Inout Mode) Unwanted aliases examples –Collisions between actual parameters. Example in C++ void fun (int &first, int &second) Now consider call of: fun(total, total); first and second are now aliases. –Collisions between array elements. Consider call of: fun( list[i], list[j] ); If i and j are equal, then first and second are now aliases. –Collisions between formal parameters and nonlocal variables that are visible. Example in C int * global; void main() { … sub(global); } void sub(int * param) {….} Param and global are now aliases.

27 1-27 Parameter Passing Methods of Major Languages Pascal provides for two: –X: integer is a pass-by-value –Var X: integer is a pass-by-reference C (from Algol 68) –Pass-by-value only, but pass-by-reference is achieved by using pointers as parameters Actual parameter is &i in the call -- subA(&i); Formal parameter is *x in the function heading -- subA(int *x); C++ –Has a special pointer type called reference type for pass-by-reference, which are implicitly dereferenced. void fun (const int & p1, int p2, int &p3) {…} where, p1 and p3 are both implicitly dereferenced and p1 cannot be changed. Java –All parameters are passed are passed by value. However, since you are passing a reference in the case of Object parameters, you are giving write-access to the object.

28 Copyright © 2007 Addison-Wesley. All rights reserved.1-28 Parameter Passing Methods of Major Languages (continued) Ada –Three semantics modes of parameter transmission: in, out, in out; in is the default mode –Formal parameters declared out can be assigned but not referenced declared in can be referenced but not assigned declared in out parameters can be referenced and assigned –Example : Procedure Adder (A: in out Integer; B : in Integer; C : out Float) Fortran 95 - Parameters can be declared to be in, out, or inout mode –Example: Subroutine Adder(A, B, C) Integer, Intent(Inout) :: A Integer, Intent(In) :: B Integer, Intent(out) :: C C# - Default method: pass-by-value –Pass-by-reference is specified by preceding both a formal parameter and its actual parameter with ref –Example : void sumer (ref int oldSum, int newOne) {..} call is sumer(ref sum, newValue); PHP: very similar to C# Perl: all actual parameters are implicitly placed in a predefined array named @_ Python and Ruby use pass-by-assignment (all data values are objects)

29 Copyright © 2007 Addison-Wesley. All rights reserved.1-29 Implementing Parameter-Passing Methods In most languages parameter transmissions takes place through the run-time stack. –Pass-by-value actual parameters have their values copied into those stack locations which serve as storage for their corresponding formal parameters. –Pass-by-result actual parameters are assigned the final value of their corresponding formal parameters when the subprogram terminates. –Pass-by-value-result have their values copied into those stack locations which serve as storage for their corresponding formal parameters, where The formal parameters are used as local variables. When the subprogram terminates, the final values of these locals are copied back into the actual parameters as with pass-by-result. –Pass-by-reference is the simplest to implement; only an address is placed in the stack. Subtle but fatal errors can occur with pass-by-reference and pass-by-value-result. –A formal parameter corresponding to a constant can mistakenly be changed.

30 Main calls sub with actual parameters: w, x, y and z Which correspond to the actual parameters a, b, c and d in function sub. void sub(int a, int b, int c, int d) with the following parameter passing mechanisms: (by-value, by-result, by-value-result, by-reference)

31 Copyright © 2007 Addison-Wesley. All rights reserved.1-31 Implementing Parameter-Passing Methods Subtle but fatal errors can occur with pass-by-reference and pass- by-value-result if implementations do not prevent their occurrence. –A formal parameter corresponding to a constant can mistakenly be changed. –Example: Hypothetical Subprogram uses pass-by-reference Sub1( Integer: P1, Integer: P2) begin P1 = 8 P2 = 5 end. Calling program (assume that b is 10 Sub1(a, 10); result = b * 10; // result will be 50 vs. 100 –Actually happened in Fortran IV –C and C++ provide for the typing of formal parameters as pointers to constants to allow for the efficiency of pass-by-reference with the security of pass by value. Even if the actual parameter is not a constant it is coerced to a constant.

32 Copyright © 2007 Addison-Wesley. All rights reserved.1-32 Type Checking Parameters Considered very important for reliability. No type checking required in FORTRAN 77 and original C. C89 the user can choose. –Prototypes are used for this purpose, where formal parameters can also be defined as: double sin(double x) {…} –Vs. double sin(x) double x; {…} C99 and C++ all functions must be in prototype form. Always required in Pascal, FORTRAN 90, Java, and Ada. Relatively new languages Perl, JavaScript, and PHP do not require type checking. In Python and Ruby, variables do not have types (objects do), so parameter type checking is not possible.

33 Copyright © 2007 Addison-Wesley. All rights reserved.1-33 Multidimensional Arrays as Parameters If a multidimensional array is passed to a subprogram and the subprogram is separately compiled, the compiler needs to know the declared size of that array to build the storage mapping function

34 Copyright © 2007 Addison-Wesley. All rights reserved.1-34 Multidimensional Arrays as Parameters: C and C++ Programmer is required to include the declared sizes of all but the first subscript in the actual parameter Disallows writing flexible subprograms Solution: pass a pointer to the array and the sizes of the dimensions as other parameters; the user must include the storage mapping function in terms of the size parameters

35 1-35 Pass-by-Name (Inout Mode) The actual parameter is, in effect, textually substituted for the corresponding formal parameter in all of its occurrences in the subprogram. Not part of any widely used language. –Used at compile time by the macros in assembly languages –Used for generic parameters of the generic programs of C++ and Ada. Allows flexibility in late binding

36 Copyright © 2007 Addison-Wesley. All rights reserved.1-36 Multidimensional Arrays as Parameters: Ada, Java and C# Ada – not a problem –Constrained arrays – size is part of the array’s type –Unconstrained arrays - declared size is part of the object declaration Java and C# –Similar to Ada –Arrays are objects; they are all single-dimensioned, but the elements can be arrays –Each array inherits a named constant ( length in Java, Length in C#) that is set to the length of the array when the array object is created

37 Copyright © 2007 Addison-Wesley. All rights reserved.1-37 Multidimensional Arrays as Parameters: Fortran Formal parameter that are arrays have a declaration after the header –For single-dimension arrays, the subscript is irrelevant –For multidimensional arrays, the sizes are sent as parameters and used in the declaration of the formal parameter, so those variables are used in the storage mapping function

38 Copyright © 2007 Addison-Wesley. All rights reserved.1-38 Design Considerations for Parameter Passing Two important considerations –Efficiency –One-way or two-way data transfer But the above considerations are in conflict –Good programming suggest limited access to variables, which means one-way whenever possible –But pass-by-reference is more efficient to pass structures of significant size

39 Copyright © 2007 Addison-Wesley. All rights reserved.1-39 Parameters that are Subprogram Names It is sometimes convenient to pass subprogram names as parameters Issues: 1.Are parameter types checked? 2.What is the correct referencing environment for a subprogram that was sent as a parameter?

40 Copyright © 2007 Addison-Wesley. All rights reserved.1-40 Parameters that are Subprogram Names: Parameter Type Checking C and C++: functions cannot be passed as parameters but pointers to functions can be passed and their types include the types of the parameters, so parameters can be type checked FORTRAN 95 type checks Ada does not allow subprogram parameters; an alternative is provided via Ada’s generic facility Java does not allow method names to be passed as parameters

41 Copyright © 2007 Addison-Wesley. All rights reserved.1-41 Parameters that are Subprogram Names: 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

42 Copyright © 2007 Addison-Wesley. All rights reserved.1-42 Overloaded Subprograms An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment –Every version of an overloaded subprogram has a unique protocol C++, Java, C#, and Ada include predefined overloaded subprograms In Ada, the return type of an overloaded function can be used to disambiguate calls (thus two overloaded functions can have the same parameters) Ada, Java, C++, and C# allow users to write multiple versions of subprograms with the same name

43 Copyright © 2007 Addison-Wesley. All rights reserved.1-43 Generic Subprograms A generic or polymorphic subprogram takes parameters of different types on different activations Overloaded subprograms provide ad hoc polymorphism A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism - A cheap compile-time substitute for dynamic binding In Ada, generic subprograms are instantiated explicitly In C++, they are instantiated implicitly, when the subprogram is named in a call or when its address is taken with the & operator

44 Copyright © 2007 Addison-Wesley. All rights reserved.1-44 Generic Subprograms (continued) Java 5.0 - Differences between generics in Java 5.0 and those of C++ and Ada: 1. Generic parameters in Java 5.0 must be classes 2. Java 5.0 generic methods are instantiated just once as truly generic methods 3. Restrictions can be specified on the range of classes that can be passed to the generic method as generic parameters 4. Wildcard types of generic parameters

45 Copyright © 2007 Addison-Wesley. All rights reserved.1-45 Generic Subprograms (continued) C# 2005 - Supports generic methods that are similar to those of Java 5.0 - One difference: actual type parameters in a call can be omitted if the compiler can infer the unspecified type

46 Copyright © 2007 Addison-Wesley. All rights reserved.1-46 Examples of parametric polymorphism: C++ template Type max(Type first, Type second) { return first > second ? first : second; } The above template can be instantiated for any type for which operator > is defined int max (int first, int second) { return first > second? first : second; }

47 Copyright © 2007 Addison-Wesley. All rights reserved.1-47 Design Issues for Functions Are side effects allowed? –Parameters should always be in-mode to reduce side effect (like Ada) What types of return values are allowed? –Most imperative languages restrict the return types –C allows any type except arrays and functions –C++ is like C but also allows user-defined types –Ada subprograms can return any type (but Ada subprograms are not types, so they cannot be returned) –Java and C# methods can return any type (but because methods are not types, they cannot be returned) –Python and Ruby treat methods as first-class objects, so they can be returned, as well as any other class

48 Copyright © 2007 Addison-Wesley. All rights reserved.1-48 User-Defined Overloaded Operators Operators can be overloaded in Ada, C++, Python, and Ruby An Ada example function "*" (A,B: in Vec_Type): return Integer is Sum: Integer := 0; begin for Index in A'range loop Sum := Sum + A(Index) * B(Index) end loop return sum; end "*"; … c = a * b; -- a, b, and c are of type Vec_Type

49 Copyright © 2007 Addison-Wesley. All rights reserved.1-49 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

50 Copyright © 2007 Addison-Wesley. All rights reserved.1-50 Coroutines Illustrated: Possible Execution Controls

51 Copyright © 2007 Addison-Wesley. All rights reserved.1-51 Coroutines Illustrated: Possible Execution Controls

52 Copyright © 2007 Addison-Wesley. All rights reserved.1-52 Coroutines Illustrated: Possible Execution Controls with Loops

53

54

55 Copyright © 2007 Addison-Wesley. All rights reserved.1-55 Summary A subprogram definition describes the actions represented by the subprogram Subprograms can be either functions or procedures Local variables in subprograms can be stack- dynamic or static Three models of parameter passing: in mode, out mode, and inout mode Some languages allow operator overloading Subprograms can be generic A coroutine is a special subprogram with multiple entries

56 Copyright © 2007 Addison-Wesley. All rights reserved.1-56 Ruby Blocks Ruby includes a number of iterator functions, which are often used to process the elements of arrays Iterators are implemented with blocks, which can also be defined by applications Blocks are attached methods calls; they can have parameters (in vertical bars); they are executed when the method executes a yield statement def fibonacci(last) first, second = 1, 1 while first <= last yield first first, second = second, first + second end puts "Fibonacci numbers less than 100 are:" fibonacci(100) {|num| print num, " "} puts


Download ppt "ISBN 0-321-49362-1 Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms."

Similar presentations


Ads by Google