290 likes | 668 Views
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
E N D
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 • of the called subprogram • only one subprogram will execute at any given • time • control always returns to the caller when the • subprogram terminates
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
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
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.
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.
Subprogram Terminology • The subprogram header is the first line of the subprogram definition. It specifies: • procedure or function • subprogram name • parameters (if any)
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;
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
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; }
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
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
Pascal - forward declarations function average(p, q:integer): real; forward; C/C++ - function prototypes float average(int p, int q); Subprogram Terminology:subprogram declarations
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
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; }
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
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?