CS 403 Programming Languages Class 14 October 12
CS 403 - Programming Languages Class 14 October 12, 2000 1
Today’s Agenda Finish Chapter 8 Start Chapter 9 Assignment: Read chapter 9 for today & Tuesday n Prepare for Exam next Thursday. Announcement: Exam is Oct 19, not Oct 12. Exam will cover chapters 1 -9. n CS 403, Class 14 2
Project 1 Writing Assignment Compare string handling in C++ and Ada. Remember: This is part of the W designation for this course, so you will be graded on writing style as well as content! Due October 26 th. Assignment on web site. CS 403, Class 14 3
Reminder Advising period: Oct 16 -27 Please update your “CS Students” links & website. Update your links at http: //cs. ua. edu/1 Students. asp Build your own website on the CS server using Front. Page. Directions are at http: //cs. ua. edu/9 Student. Web. Sites. shtm CS 403, Class 14 4
Chapter 8: Already Covered… Basic Subprogram terms Actual/Formal Parameters Local environment Parameter passing modes & implementations Type checking parameters Multidimensional Arrays as Parameters Design Considerations for Parameter Passing CS 403, Class 14 5
Parameters that are Subprogram Names Issues: 1. Are parameter types checked? • Early Pascal and FORTRAN 77 do not • Later versions of Pascal, Modula-2, and FORTRAN 90 do • Ada does not allow subprogram parameters • C and C++ - pass pointers to functions; parameters can be type checked CS 403, Class 14 6
Parameters that are Subprogram Names Issues: 2. What is the correct referencing environment for a subprogram that was sent as a parameter? - Possibilities: a. It is that of the subprogram that enacts it. - Shallow binding b. It is that of the subprogram that declared it. - Deep binding c. It is that of the subprogram that passed it. - Ad hoc binding (Has never been used) CS 403, Class 14 7
Parameters that are Subprogram Names Issues: 2. What is the correct referencing environment for a subprogram that was sent as a parameter? (cont’d) • For static-scoped (block structured) languages, deep binding is most natural. • For dynamic-scoped languages, shallow binding is most natural. CS 403, Class 14 8
Subprogram Names as Parameters -- Exercise Example: sub 1 sub 2 x : = 42; // what x? sub 3 call sub 4(sub 2) sub 4(subx) call subx begin call sub 3 end; What is the referencing environment of sub 2 when it is called in sub 4? (for deep or shallow binding) CS 403, Class 14 9
Overloaded Subprograms Def: An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment. C++ and Ada have overloaded subprograms built-in, and users can write their own overloaded subprograms. CS 403, Class 14 10
Generic Subprograms A generic or polymorphic subprogram is one that 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 CS 403, Class 14 11
Generic Subprograms Examples of parametric polymorphism 1. Ada • Types, subscript ranges, constant values, etc. , can be generic in Ada subprograms and packages • Ada generics are used to provide the functionality of parameters that are subprograms; generic part is a subprogram. CS 403, Class 14 12
Generic Subprograms Examples of parametric polymorphism 2. C++ • Templated functions e. g. template <class Type> Type max(Type first, Type second) { return first > second ? first : second; } • C++ template functions are instantiated implicitly when the function is named in a call or when its address is taken with the & operator. CS 403, Class 14 13
Generic Subprograms C++ Example: template <class Type> void generic_sort(Type arr[], int len) {. . . } //** end of generic_sort Example use: float flt_arr[100]; . . . generic_sort(flt_arr, 100); // Implicit // instantiation CS 403, Class 14 14
Compilation Issues Def: Independent compilation is compilation of some of the units of a program separately from the rest of the program, without the benefit of interface information. Def: Separate compilation is compilation of some of the units of a program separately from the rest of the program, using interface information to check the correctness of the interface between the two parts. Language Examples: • FORTRAN II to FORTRAN 77 - independent • FORTRAN 90, Ada, Modula-2, C++ - separate • Pascal - allows neither CS 403, Class 14 15
- Slides: 15