Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.

Similar presentations


Presentation on theme: "© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003."— Presentation transcript:

1 © 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003

2 © 2003 G. Drew Kessler and William M. Pottenger2 Subroutines Method of process abstraction Supports modular design Improves readability Allows code reuse Conserves memory Saves coding time Characteristics: Single Entry Calling program blocks during subroutine call Control always returns to caller Except: co-routines, concurrent execution

3 © 2003 G. Drew Kessler and William M. Pottenger3 Design Issues & Mechanisms Method of parameter passing Function vs. procedure Mathematical function vs. program statement Local and non-local referencing environments Overloaded subroutines Generic subroutines Separate compilation, independent compilation Require info for type checking separately compiled subroutines? Aliasing and side effect problems

4 © 2003 G. Drew Kessler and William M. Pottenger4 Subroutine Parameters Parameters Provide access to data outside of subroutine  ‘Safer’ than using global variables Allows for specialization of computational tasks Actual parameters: values given at subprogram call site Formal parameters are given in subprogram definition

5 © 2003 G. Drew Kessler and William M. Pottenger5 Subprogram Definition Subprogram header: first line of definition Provides name, list of parameters (0 or more) May use special word (“procedure”, “function”, “subroutine”), or context (as in C) Parameter profile: provides #, order, and types of formal parameters Parameter protocol: profile plus subroutine return type Subprogram declaration or prototype: provides info needed by compiler to do type-checking, before definition (some languages need forward qualifier)

6 © 2003 G. Drew Kessler and William M. Pottenger6 Parameter Designs Association between formal and actual parameters: Positional parameters void sort(int list[], int size, bool ascending); sort(numbers, n, true); // call to sort() Keyword parameters sort(size=>n, list=>numbers, ascending=>true); //call Default parameters? void sort(int list[], int size=100, bool ascending=true); sort(numbers); // call to sort() Parameter list length fixed? (In C++, “...” is 0 or more) void sort(int list[], …); // avoid type checking of … sort(numbers, size); // call to sort() sort(…); // Will this work? What does it mean? What commonly used C function employs this ellipsis operator? How does it work? Parameters (& subroutine parameters) type-checked?

7 © 2003 G. Drew Kessler and William M. Pottenger7 Local and Non-Local Referencing Environments Local variables defined inside subroutine Formal parameters are usually local variables Non-local variables are either global or are variables that are in scope but local to another subroutine Local variable storage Static  Allows direct access  Provides for history sensitive subroutines Stack-dynamic  Allows recursion  Provides memory savings

8 © 2003 G. Drew Kessler and William M. Pottenger8 Parameter Passing Methods Semantic modes: In, out, in out Implementation methods of data transfer: value, access path (a.k.a. reference) Methods: Pass-by-value (in) Pass-by-result (out)  We’re not referring to the subroutine’s return value here! Pass-by-value-result (in out, value)  (a.k.a. pass-by-copy) Pass-by-reference (in out, reference) Pass-by-name (in out, other)

9 © 2003 G. Drew Kessler and William M. Pottenger9 Parameter Passing Examples CallerSubroutine Pass-by-value Pass-by-result Pass-by-value- result Pass-by-reference Actual parameterFormal parameter

10 © 2003 G. Drew Kessler and William M. Pottenger10 Parameter Passing in Various Languages Wide variety of implementations Fortran: always by reference Pascal: programmers choice (var) C: by value (except for arrays/explicit pointers) C++: by value, C++ ‘reference’ or as with C Java: ‘built-ins’ by value; others by reference

11 © 2003 G. Drew Kessler and William M. Pottenger11 Parameter Passing in Ada In, In/Out, Out: Ada parameter passing can be implemented by value or reference…  Morgan Kaufmann (Figure reproduced by permission of author/publisher)

12 © 2003 G. Drew Kessler and William M. Pottenger12 Call-by-Name Example (ALGOL 60) real procedure sum ( k, l, u, ak) value l, u; integer k, l, u; real ak; begin real s; s := 0; for k := l step 1 until u do s := s + ak end Use: x := sum (i, 1, n, V[i]) x := sum (i, 1, m, sum(j, 1, n, A[i, j])) (Jensen’s Device)

13 © 2003 G. Drew Kessler and William M. Pottenger13 Call-by-Name Problem Consider a swap routine: procedure swap (j, k) integer j, k; begin integer t; t := j; j := k; k := t; end; And, consider this: i := 1, A[1]:=2, A[2]:=8 swap(i, A[i]) Do other methods have a problem with this?

14 © 2003 G. Drew Kessler and William M. Pottenger14 Overloaded subprograms System and user-defined Same name, different operation Right operation identified by parameter and return types Return type distinction not available if mixed-mode expressions allowed For example: int f (int i) {…;} float f (int i) {…;} int i = j = 7; float result = f(i) + f(j); // Which f() is called?

15 © 2003 G. Drew Kessler and William M. Pottenger15 Generic subroutines Provide routine, where parameter and/or variable types are defined at a later time. New instance of routine with specific type created when needed or explicitly Ada generic units C++ templates template Type findMax(Type list[], int size) { Type max = list[0]; for (int i=1; i<size; i++) if (list[i]<max) max = list[i]; return max; }

16 © 2003 G. Drew Kessler and William M. Pottenger16 Generic Subroutines/Modules  Morgan Kaufmann (Figure reproduced by permission of author/publisher) Is it wise to return item by value?

17 © 2003 G. Drew Kessler and William M. Pottenger17 Inline Expansion C++ and Ada support ‘inline expansion’ C++: inline int max (int a, int b) { return a > b ? a : b; } Ada: pragma inline (max); Avoids potential problems with macros: e.g., –#define MAX(a,b) ((a) > (b) ? (a) : (b)) –Doesn’t work when ‘calling’ MAX(x++,y++) Costs more – increase in code size/compile time

18 © 2003 G. Drew Kessler and William M. Pottenger18 Co- Routines


Download ppt "© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003."

Similar presentations


Ads by Google