1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Bài giảng Chapter 9 subprograms

73 6 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Subprograms
Trường học Addison-Wesley
Năm xuất bản 2006
Định dạng
Số trang 73
Dung lượng 560,81 KB

Nội dung

Chapter Subprograms ISBN 0-321-33025-0 Chapter Topics • • • • • • Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprogram Names • • • • • Overloaded Subprograms Generic Subprograms Design Issues for Functions User-Defined Overloaded Operators Coroutines Copyright © 2006 Addison-Wesley All rights reserved 1-2 Introduction • Two fundamental abstraction facilities – Process abstraction  Emphasized from early days – Data abstraction  Emphasized in the 1980s Copyright © 2006 Addison-Wesley All rights reserved 1-3 Fundamentals of Subprograms • Each subprogram has a single entry point • The calling (sub)program (caller) is suspended during execution of the called subprogram (callee) • Control always returns to the caller when the callee’s execution terminates Copyright © 2006 Addison-Wesley All rights reserved 1-4 Basic Definitions • A subprogram definition describes the interface and the actions of the subprogram • A subprogram header is the first part of the definition, including the name, the kind of subprogram, and the formal parameters • The parameter profile (signature) of a subprogram is the number, order, and types of its parameters • The protocol is a subprogram’s parameter profile and its return type, if it is a function Copyright © 2006 Addison-Wesley All rights reserved 1-5 Basic Definitions (cont.) • A subprogram declaration provides the protocol, but not the body, of the subprogram – Function declarations in C/C++ are often called prototypes – Subprogram declarations in Ada/Pascal are sometimes called forward declarations • A formal parameter is a dummy variable listed in the subprogram header and used in the subprogram • An actual parameter represents a R-value or L-value used in the subprogram call statement Copyright â 2006 Addison-Wesley All rights reserved 1-6 Parameters ã There are two ways that a subprogram can access to the data – Through direct access to nonlocal variables – Through parameter passing • Parameter passing is more flexible – Parameter passing is a parameterized computation – Nonlocals that are visible to the subprogram where access to them is desired or not  poor reliability Copyright © 2006 Addison-Wesley All rights reserved 1-7 Actual/Formal Parameter • Positional parameters – 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 – A good method for relatively short parameter lists • Keyword parameters – The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter – Parameters can appear in any order Copyright © 2006 Addison-Wesley All rights reserved 1-8 Actual/Formal Parameter (cont.) • Example: Ada SUMER(LENGTH => MY_LENGTH, LIST => MY_ARRAY, SUM => MY_SUM); where LENGTH, LIST and SUM are formal parameter • Example: Ada and FORTRAN 90 allow to mix positional and keyword parameters in a call SUMER(MY_LENGTH, LIST => MY_ARRAY, SUM => MY_SUM); Copyright © 2006 Addison-Wesley All rights reserved 1-9 Formal Parameter Default Values • In certain languages (e.g., C++, Ada), formal parameters can have default values • In C++, default parameters must appear last because parameters are positionally associated • Example: Ada function COMP(A:FLOAT; B:INTEGER := 1; C:FLOAT) return FLOAT; PAY := COMP(20.0, C => 0.75) • Example: C++ float comp(float A, float C, int B = 1); pay = comp(20, 0.75); Copyright © 2006 Addison-Wesley All rights reserved 1-10 Example - Ada procedure MAIN is type FLOAT_VECTOR is array (INTEGER range ) of FLOAT; type INT_VECTOR is array (INTEGER range ) of INTEGER; … procedure SORT( FLOAT_LIST : in out FLOAT_VECTOR; LOWER_BOUND : in INTEGER; UPPER_BOUND : in INTEGER) is … end SORT; procedure SORT( INT_LIST : in out INT_VECTOR; LOWER_BOUND : in INTEGER; UPPER_BOUND : in INTEGER) is … end SORT; … end MAIN; Copyright © 2006 Addison-Wesley All rights reserved 1-59 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 provides parametric polymorphism Copyright © 2006 Addison-Wesley All rights reserved 1-60 Ada generic procedure generic type INDEX_TYPE is (); type ELEMENT_TYPE is private; type VECTOR is array (INDEX_TYPE range ) of ELEMENT_TYPE; procedure GENERIC_SORT(LIST : in out VECTOR); procedure GENERIC_SORT(LIST : in out VECTOR) is TEMP: ELEMENT_TYPE; begin for TOP in LIST'FIRST INDEX_TYPE'PRED(LIST'LAST) loop for BOTTOM in INDEX_TYPE'SUCC(TOP) LIST'LAST loop if LIST(TOP) > LIST(BOTTOM) then TEMP := LIST(TOP); LIST(TOP) := LIST(BOTTOM); LIST(BOTTOM) := TEMP; end if; end loop; — for BOTTOM end loop; - for TOP end GENERIC_SORT; … procedure INTEGER_SORT is new GENERIC_SORT(INDEX_TYPE => INTEGER; ELEMENT_TYPE => INTEGER; VECTOR => INT_ARRAY); Copyright © 2006 Addison-Wesley All rights reserved 1-61 Ada: Parameters that are Subprogram Names generic with function FUN(X : FLOAT) return FLOAT; procedure INTEGRATE( LOWERBD : in FLOAT; UPPERBD : in FLOAT; RESULT : out FLOAT); procedure INTEGRATE( LOWERBD : in FLOAT; UPPERBD : in FLOAT; RESULT : out FLOAT) is FUNVAL : FLOAT; begin …; FUNVAL := FUN(LOWERBD); … end; … procedure INTEG_FUN is new INTEGRATE(FUN => FUN1); Copyright © 2006 Addison-Wesley All rights reserved 1-62 Examples of parametric polymorphism: C++ template void generic_sort(Type list[], int len) { int i, j; Type temp; for (i = 0; i < len - 1; i++) for (j = i + 1; j < len; j++) if (list[i] > list[j]) swap(list[i], list[j]); } … float list[10]; generic_sort(list, 10); • The above template can be instantiated for any type for which operator > is defined Copyright © 2006 Addison-Wesley All rights reserved 1-63 Design Issues for Functions • Are side effects allowed? – Parameters should always be in-mode to reduce side effect (Ada) • What types of return values are allowed? – Most imperative languages restrict the return types – FORTRAN 77 and Pascal functions allow only unstructured types to be returned – C/C++ allows any type except arrays and functions – Ada allows any type – Java and C# not have functions but methods can have any type Copyright © 2006 Addison-Wesley All rights reserved 1-64 Access nonlocal environments • The nonlocal variables of a subprogram are those that are visible but not declared in the subprogram • Global variables are those that may be visible in all of the subprograms of a program • Methods: – Fortran COMMON block  This is a way to specify that certain variables should be shared among certain subroutines  In general, the use of COMMON blocks should be minimized Copyright © 2006 Addison-Wesley All rights reserved 1-65 Access nonlocal environments (cont.) – External declarations - C  Subprograms are not nested  Globals are created by external declarations (they are simply defined outside any function)  Declarations (not definitions) give types to externally defined variables (and say they are defined elsewhere) Copyright © 2006 Addison-Wesley All rights reserved 1-66 User-Defined Overloaded Operators • Nearly all programming languages have overloaded operators • Users can overload operators in C++ and Ada 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 are of type Vec_Type Copyright © 2006 Addison-Wesley All rights reserved 1-67 Coroutines • Coroutines have multiple entry points, which are controlled by the coroutines themselves • Also called symmetric control: caller and callee coroutines are on a more equal basis • A coroutine call is named a resume – The first resume of a coroutine is to its beginning – The 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 Copyright © 2006 Addison-Wesley All rights reserved 1-68 The Execution of Coroutines • Typically, coroutines are created in an application by the master unit, which is not a coroutine • When created, coroutines execute their initialization code and then return control to that master unit • When all of coroutines are constructed, the master resumes one of the coroutines, and the coroutines then resume each other in some order until their work is completed • If the execution of a coroutine reaches the end of its code section, control is transferred to the master unit that created it Copyright © 2006 Addison-Wesley All rights reserved 1-69 Coroutines Illustrated: Possible Execution Controls Copyright © 2006 Addison-Wesley All rights reserved 1-70 Coroutines Illustrated: Possible Execution Controls Copyright © 2006 Addison-Wesley All rights reserved 1-71 Coroutines Illustrated: Possible Execution Controls with Loops Copyright © 2006 Addison-Wesley All rights reserved 1-72 Summary • A subprogram definition describes the actions represented by the subprogram • Subprograms can be either functions or procedures • Local variables in subprograms can be stackdynamic 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 Copyright © 2006 Addison-Wesley All rights reserved 1-73 .. .Chapter Topics • • • • • • Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing... Environments Parameter-Passing Methods Parameters That Are Subprogram Names • • • • • Overloaded Subprograms Generic Subprograms Design Issues for Functions User-Defined Overloaded Operators Coroutines Copyright... Emphasized from early days – Data abstraction  Emphasized in the 198 0s Copyright © 2006 Addison-Wesley All rights reserved 1-3 Fundamentals of Subprograms • Each subprogram has a single entry point •

Ngày đăng: 23/03/2022, 08:37

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w