1 / 18

Subprograms

Subprograms. Subprograms. The use of subprograms enables process abstraction code reuse. Subprogram Characteristics. The subprograms discussed in the chapter are assumed to have certain basic characteristics single entry point calling program is suspended during execution

derica
Download Presentation

Subprograms

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

  2. Subprograms • The use of subprograms enables • process abstraction • code reuse

  3. Subprogram Characteristics • The subprograms discussed in the chapter are • assumed to have certain basic characteristics • single entry point • calling program is suspended during execution • of the called subprogram • only one subprogram will execute at any given • time • control always returns to the caller when the • subprogram terminates

  4. Subprogram Categories Procedures - collections of statements that define parameterized computations. They can be thought of as defining new statements Functions - defines new user-defined operators

  5. Subprogram Categories • Procedures - • collections of statements that define • parameterized computations . They can be • thought of as defining new statements. • Can produce results in the calling program by: • modifying global variables • using formal parameters which allow the transfer of data back to the caller

  6. Subprogram Categories Functions - defines new user-defined operators. They are called by the appearance of their names in expressions along with the actual parameters. The value produced by a functions execution is returned to the calling code, effectively by replacing the call. Functions should not produce side effects.

  7. Subprogram Terminology The subprogram call is the explicit request that the subprogram be executed. The subprogram definition describes the actions of the subprogram abstraction. It defines the number and type of parameters and local variables, and the statements to be executed when the subprogram is invoked.

  8. Subprogram Terminology • The subprogram header is the first line of the subprogram definition. It specifies: • procedure or function • subprogram name • parameters (if any)

  9. Subprogram Terminology:Pascal example subprogram call : x := average(a, b); subprogram definition : function average(p, q:integer): real; var sum : integer; begin sum := p + q; average := sum / 2.0 end;

  10. Subprogram Terminology:FORTRAN example subprogram call : X = AVERAG(A, B) subprogram definition : REAL FUNCTION AVERAG (P, Q) INTEGER P, Q, SUM SUM = P + Q AVERAG = SUM / 2.0 RETURN END

  11. Subprogram Terminology:C example subprogram call : x = average(a, b); subprogram definition : float average(int p, int q) { int sum; sum = p + q; return sum / 2.0; }

  12. Subprogram Terminology: The subprogram declaration provides type checking information but does not include subprogram bodies FORTRAN - function return type declarations C/C++ - prototypes Pascal - forward declarations

  13. Subprogram Terminology:FORTRAN - function return type declarations PROGRAM MAIN INTEGER A, B, C REAL NUMAVG PRINT *, ‘ENTER TWO INTEGERS’ READ *, A, B C = NUMAVG (A,B) PRINT *, ‘The sum is ‘, C END REAL FUNCTION NUMAVG(X, Y) INTEGER X, Y NUMAVG = (X + Y) / 2.0 RETURN END

  14. Pascal - forward declarations function average(p, q:integer): real; forward; C/C++ - function prototypes float average(int p, int q); Subprogram Terminology:subprogram declarations

  15. Parameters Formal parameters - parameters in the subprogram header Actual parameters - parameters in the subprogram call, to be bound to the formal parameters of the subprogram

  16. Parameters Actual parameters x = average(a, b); Formal parameters float average(int p, int q){ int sum; float avg; sum = p + q; avg = sum / 2.0; return avg; }

  17. Correspondence between Formal and Actual Parameters positional parameters - (Pascal, C, FORTRAN 77) the binding of actual to formal parameters is determined by position keyword parameters - (Ada, FORTRAN 90) the name of the formal parameter is specified with the actual parameter default valued parameters - (C++, Ada, FORTRAN 90, Java) a default value is used if no actual parameters are passed

  18. Can subprograms be overloaded? generic? Subprograms: design issues • What parameter passing methods are used? • Are the types of the actual parameters checked • against the formal parameters? • Are local variables statically or dynamically allocated? • If subprograms can be passed as parameters: • What is the referencing environment for the subprogram? • Are the types of parameters checked in the calls? • Is either separate or independent compilation • possible?

More Related