1 / 76

CPS 506 Comparative Programming Languages

CPS 506 Comparative Programming Languages. Sub-program and Parameter Passing. Topics. Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprograms Overloaded Subprograms

nerina
Download Presentation

CPS 506 Comparative Programming Languages

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. CPS 506Comparative Programming Languages Sub-program and Parameter Passing

  2. Topics • Introduction • Fundamentals of Subprograms • Design Issues for Subprograms • Local Referencing Environments • Parameter-Passing Methods • Parameters That Are Subprograms • Overloaded Subprograms • User-Defined Overloaded Operators • Generic Subprograms • Design Issues for Functions • Co-routines

  3. Fundamentals of Subprograms • Three fundamentals • 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

  4. Basic Definitions • Two kinds of subprograms • Function • Procedure • A subprogram definition describes • The interface • The actions • A subprogram call is an explicit request that the subprogram be executed

  5. Basic Definitions • In Python, function definitions are executable; in all other languages, they are non-executable # map.py def map( fun, list ): nlist = [] for item in list: nlist.append( fun( item ) ) return nlist # Make a sample test function def increment(x): return x+1 # Test them out! map( increment, [1,2,3,4,5] ) # should return [2,3,4,5,6]

  6. Basic Definitions • A subprogram header is the first part of the definition, including • Name • Kind of subprogram • Formal parameters • The parameter profile (signature) of a subprogram is the • Number • Order • and Types of its parameters • The protocol of subprogram is • Parameter profile • and its Return type (if it is a function)

  7. 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

  8. Basic Definitions (continued) • Functions as first-class entities • Can be stored in data structures • Pass as parameters • Returned from functions • Lua (anonymous functions) function cube(x) return x * x * x end cube = function (x) return x * x * x end

  9. 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 • All the languages support this method of parameter binding

  10. Actual/Formal Parameter Correspondence • Keyword • The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter • Advantage: Parameters can appear in any order, thereby avoiding parameter correspondence errors • Disadvantage: User must know the formal parameter’s names • Python can use this type of parameter binding • Python, Ada, Fortran 95 can have both in one function call

  11. Formal Parameter Default Values • In certain languages (e.g., C++, Python, Ruby, Ada, PHP), formal parameters can have default values (if no actual parameter is passed) • In C++, default parameters must appear last because parameters are positionally associated • C++ float compute_pay(float income, float tax_rate, int exemptions = 1) pay = compute_pay(20000.0, 0.15); • Python Def compute_pay(income, exemptions = 1, tax_rate) pay = compute_pay(20000.0,tax_rate = 0.15)

  12. Formal Parameter Default Values (con’t) • Variable number of parameters • C# methods can accept a variable number of parameters as long as they are of the same type—the corresponding formal parameter is an array preceded by params Public void DisplayList(paramsint[] list) { foreach (int next in list) { Console.WriteLine(“Next value {0}”, next); } } MyclassmyObject = new Myclass; int[] myList = new int[6] {2,4,6,8,10,12}; myObject.DisplayList(myList); myObject.DisplayList(2,4,3*x-1,17);

  13. Formal Parameter Default Values (con’t) • Variable number 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. (Details discussed in Ruby part) list = [2,4,6,8] def tester(p1,p2,p3,*p4) … end … tester(‘first’, mon =>72, tue =>68, wed =>59, *list) p1 is ‘first’ p2 is {mon =>72, tue =>68, wed =>59} p3 is 2 p4 is [4,6,8]

  14. Formal Parameter Default Values (con’t) • Variable number of parameters • In Python, the actual is a list of values and the corresponding formal parameter is a name with an asterisk (Details discussed in Python part) def fun1(p1,p2,*p3,**p4) … … Fun1(2,4,6,8, mon =72, tue =68, wed =59) p1 is 2 P2 is 4 p3 is [6,8] p4 is {‘mon’:72, ‘tue’:68, ‘wed’:59}

  15. 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

  16. Ruby Blocks # A method to compute and yield Fibonacci numbers up to # a limit def fibonacci(last) first, second = 1, 1 while first <= last yield first first, second = second, first + second end end puts "Fibonacci numbers less than 100 are:" fibonacci(100) {|num| print num, " "} puts sum = 0 fibonacci(100) {|num| sum +=num} puts

  17. Procedures and Functions • There are two categories of subprograms • Procedures are collection of statements that define parameterized computations • Two ways of producing result • Variables that are not formal parameters but are visible in both the procedure and the caller program unit • Formal parameters that allow the transfer of data to the caller (call by reference parameters)

  18. Procedures and Functions (con’t) • ... • Functions structurally resemble procedures but are semantically modeled on mathematical functions • They are expected to produce no side effects • No modification on the parameters • No modification on variables defined outside • Pure functions return only a value • In practice, program functions have side effects • Define user-defined operators (such as power) • Overload operators by defining function in Ada, Python, Ruby, C++, and C# (discussed later) • void functions in C-based languages work like procedures

  19. Design Issues for Subprograms • Are local variables static or dynamic? • 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?

  20. Local Referencing Environments • Local variables can be stack-dynamic • Bound to storage when the subprogram begins execution • Unbounded from storage when that execution terminates • Advantages • Support for recursion • Storage for locals is shared with those of inactive subprograms (great advantage for old computers with small size of memory) • Disadvantages • Allocation/de-allocation, initialization time • Indirect addressing (access only during the execution) • Subprograms cannot be history sensitive • Writing a subprogram for generating random numbers • Local variables can be static • Advantages and disadvantages are the opposite of those for stack-dynamic local variables

  21. Local Referencing Environments (con’t) • In C and C++, local variables are stack-dynamic unless specifically declared to be static int adder(int list[], intlistlen) { static int sum = 0; int count; for (count=0;count < listlen;count++) sum += list[count]; return sum; } • Java, C# and Ada have only stack-dynamic local variables

  22. Local Referencing Environments (con’t) • In Fortran 95 a subprogram can be explicitly specified to be recursive. • So the local variables are stack-dynamic by default • Force variables to be static using Save keyword Recursive subroutine sub() Integer :: Count Save, Real :: Sum ... End Subroutine sub • In Python methods, all local variables are stack-dynamic

  23. Semantic Models of Parameter Passing • In mode • No change is returned. Some parameters are just passed to the subprogram • Out mode • No parameter is passed. Just result is returned to the caller • Inout mode

  24. Models of Parameter Passing

  25. Pass-by-Value (In Mode) • The value of the actual parameter is used to initialize the corresponding formal parameter • Normally implemented by copying • Can be implemented by transmitting an access path but not recommended (enforcing write protection is not easy) • Disadvantages (if by physical move): additional storage is required (stored twice) and the actual move can be costly (for large parameters) • Disadvantages (if by access path method): must write-protect in the called subprogram and accesses cost more (indirect addressing)

  26. Pass-by-Result (Out Mode) • When a parameter is passed by result, no value is transmitted to the subprogram; the corresponding formal parameter acts as a local variable; its value is transmitted to caller’s actual parameter when control is returned to the caller, by physical move • Require extra storage location and copy operation • Potential problems • sub(p1, p1); whichever formal parameter is copied back will represent the current value of p1 • Evaluation time

  27. Pass-by-Result (Out Mode) • C# void Fixer(out intx,outint y) { x = 17; y =35; } ... f.Fixer(out a, out a); ---------------------- void DoIt(out int x, int index) { x = 17; index = 42; } ... sub = 21; f.DoIt(list[sub], sub);

  28. Pass-by-Value-Result (in-out 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

  29. Pass-by-Reference (In-out Mode) • Pass an access path • Also called pass-by-sharing • Advantage: Passing process is efficient (no copying and no duplicated storage) • Disadvantages • Slower accesses (compared to pass-by-value) to formal parameters • Additional level of indirect addressing • Potentials for unwanted side effects (collisions) • Unwanted aliases • Access to non-local variables (reduce readability and reliability)

  30. Pass-by-Reference (In-out Mode) • C++ • Collusion between actual parameters void fun(int &first, int &second) fun(total, total) first and second in fun will be aliases. • Collusion between array elements fun(list[i], list[j]) fun1(list[i], list)

  31. Pass-by-Reference (In-out Mode) • C++ • Collusion between formal parameters and non-local variables that are visible int * global; void main(){ ... sub(global); ... } void sub(int * param) { ... } param and global are aliases. All these possible aliasing situations are eliminated if pass-by-value-result is used.

  32. Pass-by-Name (In-out Mode) • By textual substitution • Formals are bound to an access method at the time of the call, but actual binding to a value or address takes place at the time of a reference or assignment • Allows flexibility in late binding • Algol procedure double(x); real x; begin x := x * 2 end; double(C[j]) is interpreted as C[j] := C[j] * 2. • Usage • Compile time for macro • Generic subprograms in C++ and Ada

  33. Implementing Parameter-Passing Methods • In most language parameter communication takes place through the run-time stack • Pass-by-reference are the simplest to implement; only an address is placed in the stack • A subtle but fatal error can occur with pass-by-reference and pass-by-value-result: a formal parameter corresponding to a constant can mistakenly be changed

  34. Function header: void sub(int a, int b, int c, int d) • Function call in main: sub(w,x,y,z) • (pass w by value, x by result, y by value-result, z by reference)

  35. Parameter Passing Methods of Major Languages • C • Pass-by-value • Pass-by-reference is achieved by using pointers as parameters • Java • All parameters are passed by value • Object parameters are passed by reference • Ada • Three semantics modes of parameter transmission • in, out, in-out • in is the default mode

  36. Parameter Passing Methods of Major Languages (continued) • Fortran 95- Parameters can be declared to be in, out, or inout mode • C#- Default method: pass-by-value • Pass-by-reference is specified by preceding both a formal parameter and its actual parameter with ref

  37. Parameter Passing Methods of Major Languages • Ada procedure Adder( A : in out Integer; B : in Integer; C : out Float) • Fortran 95 Subroutine Adder(A, B, C) Integer,Intent(Inout) :: A Integer,Intent(In) :: B Integer,Intent(Out) :: C • C# void sumer(ref intoldSum, intnewOne, out nextOne) {...} ... sumer(ref sum, newValue, nextValue);

  38. Parameter Passing Methods of Major Languages (continued) • PHP: very similar to C# • Pass-by-reference by preceding & • Perl: all actual parameters are implicitly placed in a predefined array named @_ • Python and Ruby use pass-by-assignment (all data values are objects), in effect is a pass-by-reference

  39. Type Checking Parameters • Considered very important for reliability • FORTRAN 77 and original C: none • C89 double sin(x) double x; {...} 0r double sin (double x) {...}

  40. Type Checking Parameters • Pascal, FORTRAN 90, Java, and Ada: it is always required • ANSI C and C++: choice is made by the user • Prototypes double sin(double x) {...} • C99 and C++: formal parameters in prototype form • Type checking could be avoided for some parameters by using “...” intprintf(const char* format_string, ...) At least one parameter

  41. Type Checking Parameters (con’t) • 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

  42. 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

  43. 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 • Storage-mapping function address(mat[I,j]) = address(mat[0,0]) + I * number_of_columns + j void fun(int matrix[][10]) {...} void main() { intmat[5][10]; ... Fun(mat); ... } • Disallows writing flexible subprograms

  44. Multidimensional Arrays as Parameters: Java and C# • Arrays are objects; they are all single-dimensioned, but the elements can be arrays • Each array inherits a named constant (lengthin Java, Lengthin C#) that is set to the length of the array when the array object is created

  45. 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

  46. Parameters that are Subprogram Names • It is sometimes convenient to pass subprogram names as parameters • Issues: • Are parameter types checked? • 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

  47. Parameters that are Subprogram Names: Parameter Type Checking • Adadoes not allow subprogram parameters; an alternative is provided via Ada’s generic facility (discussed later) • Java does not allow method names to be passed as parameters

  48. Parameters that are Subprogram Names • Issues: (con’t) • Referencing environment for executing the passed subprogram • In languages that allow nested subprograms • Three choices

  49. Parameters that are Subprogram Names: Referencing Environment • Three choices • 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

  50. Parameters that are Subprogram Names: Referencing Environment • Shallow binding • Output from alert(x) is 4 • Deep binding • Output from alert(x) is 1 • Ad hoc binding • Output from alert(x) is 3 function sub1() { var x; function sub2() { alert(x); }; function sub3() { var x; x=3; sub4(sub2); }; function sub4(subx) { var x; x=4; subx(); }; x=1; sub3(); };

More Related