1. Trang chủ
  2. » Giáo án - Bài giảng

Subprograms document

37 164 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

Định dạng
Số trang 37
Dung lượng 316,15 KB

Nội dung

Chapter 9 Subprograms ISBN 0-321-33025-0 Chapter 9 Topics • Introduction Fd l fSb • F un d amenta l s o f S u b programs • Design Issues for Subprograms Local Referencing Environments • Local Referencing Environments • Parameter-Passing Methods • Parameters That Are Subprogram Names • Parameters That Are Subprogram Names • Overloaded Subprograms • Generic Subprograms Generic Subprograms • Design Issues for Functions • User-Defined Overloaded Operators Copyright © 2006 Addison-Wesley. All rights reserved. 1-2 • Coroutines Introduction • Two fundamental abstraction facilities – Process abstraction  Em p hasized from earl y da y s pyy – Data abstraction  Emphasized in the 1980 s  Emphasized in the 1980 s 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 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 action s of the subprogram •A sub p ro g ram header is the first p art of the pg p definition, including the name, the kind of subprogram, and the formal parameters subprogram, and the formal parameters •The parameter profile (signature) of a subprogram ith b d d t fit t i s th e num b er , or d er , an d t ype s o f it s parame t ers •The protocol is a subprogram’s parameter profile Copyright © 2006 Addison-Wesley. All rights reserved. 1-5 and its return type, if it is a function Basic Definitions (cont.) •A subprogram declaration provides the protocol, but not the body of the subprogram but not the body , of the subprogram – Function declarations in C/C++ are often called prototypes 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 Copyright © 2006 Addison-Wesley. All rights reserved. 1-6 L -value used in the subprogram call statement Parameters • There are two ways that a subprogram can access tthdt t o th e d a t a – Through direct access to nonlocal variables Th h i – Th roug h parameter pass i ng • Parameter passing is more flexible – Parameter passing is a parameterized computation – The only way the computation can proceed on different data is to assign new values to nonlocals between calls data is to assign new values to nonlocals between calls to the subprogram – Nonlocals that are visible to the subprogram where Copyright © 2006 Addison-Wesley. All rights reserved. 1-7 Nonlocals that are visible to the subprogram where access to them is desired or not  poor reliability Actual/Formal Parameter • Positional parameters Th bi di f l f l – Th e bi n di ng o f actua l parameters to f orma l parameters is by position: the first actual parameter is bound to the first formal parameter parameter is bound to the first formal parameter and so forth – A good method for relatively short parameter lists A good method for relatively short parameter lists • Keyword parameters – The name of the formal parameter to which an The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter Copyright © 2006 Addison-Wesley. All rights reserved. 1-8 – Parameters can appear in any order Actual/Formal Parameter (cont.) • Example: Ada SUMER(LENGTH => MY_LENGTH, LIST => MY_ARRAY, SUM => MY _ SUM); _ where LENGTH, LIST and SUM are formal parameter E l Ad d FORTRAN 90 ll t i • E xamp l e: Ad a an d FORTRAN 90 a ll ow t o m i x 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 parameters can have default values • In C++, default parameters must appear last because parameters are positionally associated because parameters are positionally associated • Example: Ada f ti COMP(A FLOAT B INTEGER 1 C FLOAT) f unc ti on COMP(A : FLOAT ; B : INTEGER := 1 ; C : FLOAT) return FLOAT; PAY : = CO MP ( 2 0 . 0 , C => 0 . 7 5 ) :CO( 0 . 0 ,C 0 . 5 ) • Example: C++ float com p( float A , float C , int B = 1 ) ; Copyright © 2006 Addison-Wesley. All rights reserved. 1-10 p( , , ) pay = comp(20, 0.75); Procedures and Functions • There are two categories of subprograms – Procedures are collection of statements that define parameterized computations  Procedures provide user-defined statements – Functions structurally resemble procedures but are Functions structurally resemble procedures but are semantically modeled on mathematical functions  Functions provide user defined operators  Functions provide user - defined operators  They are expected to produce no side effects  Itifti hidfft Copyright © 2006 Addison-Wesley. All rights reserved. 1-11  I n prac ti ce, f unc ti ons may h ave s id e e ff ec t s Design Issues for Subprograms • What parameter passing methods are provided? • Are parameter types checked? • Are local variables static or dynamic? • Can subprogram definitions appear in other subprogram definitions? • What is the referencing environment of a passed subprogram? • Can subprograms be overloaded? • Can subprogram be generic? Copyright © 2006 Addison-Wesley. All rights reserved. 1-12 • Can subprogram be compiled independently? Local Referencing Environments • Local variables can be stack-dynamic – Advantages Advantages  Support for recursion  Storage for locals is shared among subprograms – Disadvantages  Allocation/deallocation, initialization time  Indirect addressing Indirect addressing  Subprograms cannot be history sensitive • Local variables can be static ff (d dd ) –More e ff icient (d irect a dd ressing ) – No run-time overhead – Cannot support recursion Copyright © 2006 Addison-Wesley. All rights reserved. 1-13 Cannot support recursion – History sensitive Local Referencing Environments (cont.) • Language Examples:  FORTRAN 77 and 90: most are static, but the implementor can choose either (user can force static with SAVE)  C/C++: both (variables declared to be static , C/C++: both (variables declared to be static , default they are stack-dynamic) ALGOL 60 Pascal Java and Ada: stack dynamic  ALGOL 60 , Pascal , Java , and Ada: stack - dynamic only Copyright © 2006 Addison-Wesley. All rights reserved. 1-14 Parameter Passing Methods • Semantic models –in mode: Formal parameters can only receive data from the corresponding actual parameter –out mode: Formal parameters can only transmit data to the actual parameter – inout mode: Formal parameters can do both • Conceptual models of transfer • Conceptual models of transfer – Physically move a value Mh Copyright © 2006 Addison-Wesley. All rights reserved. 1-15 – M ove an access pat h Models of Parameter Passing (cont.) Copyright © 2006 Addison-Wesley. All rights reserved. 1-16 Parameter Passing Methods (cont.) • Ways in which parameters are transmitted to and/or from callee – Pass - by - value Pass by value – Pass-by-result P b l l – P ass- b y-va l ue-resu l t – Pass-by-reference – Pass-by-name Copyright © 2006 Addison-Wesley. All rights reserved. 1-17 Pass-by-Value (in-mode Model) • The value of the actual parameter is used to initialize the corresponding formal parameter –Normall y im p lemented b y co py in g yp ypyg  Additional storage is required  Storage and copy operations can be costly  Storage and copy operations can be costly – Can be implemented by transmitting an access path but not recommended path but not recommended  Must write-protect in the called subprogram  A(ididdi) Copyright © 2006 Addison-Wesley. All rights reserved. 1-18  A ccesses cost more (i n di rect a dd ress i ng ) Pass-by-Result (out-mode Model) • When a parameter is passed by result – No value is transmitted to the callee – The corresponding formal parameter acts as a The corresponding formal parameter acts as a local variable; its value is transmitted to caller’s actual parameter when control is returned to the actual parameter when control is returned to the caller Require extra storage location and copy – Require extra storage location and copy operation Copyright © 2006 Addison-Wesley. All rights reserved. 1-19 Potential Problems • There can be an actual parameter collision procedure sub(y: int, z: int); … sub(x, x); … Value of x depends on order of assignments at the return • The implementor may be able to choose between two different times to evaluate the addresses of the actual parameters: at the time of the call or at the time of the return – Suppose that list[idx] is an actual parameter. If idx is chan g ed by the callee, then the address of Copyright © 2006 Addison-Wesley. All rights reserved. 1-20 g list[idx] will change between the call and the return [...]... parameter Copyright © 2006 Addison-Wesley All rights reserved 1-57 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 C++, Java C# overloaded subprograms – Example: Ada has several versions of the output function... 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... isible all of the subprograms of a program • M th d Methods: – Fortran COMMON block  Thi i a way t specify th t certain variables should This is to if that t i i bl h ld 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... rights reserved 1-44 Multidimensional Arrays as Parameters: C/C++ • Programmer is required to include the declared sizes of all but the first subscript in the actual parameter – Disallows writing flexible subprograms void fun(int matrix[][3]) • Pass a pointer to the array and the sizes of the dimensions as other parameters; the user should include the storage mapping function #define a(r, c) (*(a + ((r) . Subprogram Names • Parameters That Are Subprogram Names • Overloaded Subprograms • Generic Subprograms Generic Subprograms • Design Issues for Functions • User-Defined Overloaded Operators Copyright. Chapter 9 Subprograms ISBN 0-321-33025-0 Chapter 9 Topics • Introduction Fd l fSb • F un d amenta l s o f S u b programs • Design Issues for Subprograms Local Referencing. Storage for locals is shared among subprograms – Disadvantages  Allocation/deallocation, initialization time  Indirect addressing Indirect addressing  Subprograms cannot be history sensitive •

Ngày đăng: 26/01/2015, 10:08

TỪ KHÓA LIÊN QUAN

w