Subroutines Part 1 CSE 262 Spring 2003 2003
Subroutines (Part 1) CSE 262, Spring 2003 © 2003 G. Drew Kessler and William M. Pottenger 1
Subroutines Method of process abstraction n Supports modular design n Improves readability Allows code reuse n Conserves memory n Saves coding time Characteristics: n Single Entry n Calling program blocks during subroutine call n Control always returns to caller n Except: co-routines, concurrent execution © 2003 G. Drew Kessler and William M. Pottenger 2
Design Issues & Mechanisms Method of parameter passing Function vs. procedure n Mathematical function vs. program statement Local and non-local referencing environments Overloaded subroutines Generic subroutines Separate compilation, independent compilation n Require info for type checking separately compiled subroutines? Aliasing and side effect problems © 2003 G. Drew Kessler and William M. Pottenger 3
Subroutine Parameters n Provide access to data outside of subroutine w ‘Safer’ than using global variables n Allows for specialization of computational tasks Actual parameters: values given at subprogram call site Formal parameters are given in subprogram definition © 2003 G. Drew Kessler and William M. Pottenger 4
Subprogram Definition Subprogram header: first line of definition Provides name, list of parameters (0 or more) n 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) n © 2003 G. Drew Kessler and William M. Pottenger 5
Parameter Designs Association between formal and actual parameters: n Positional parameters void sort(int list[], int size, bool ascending); sort(numbers, n, true); // call to sort() n 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? © 2003 G. Drew Kessler and William M. Pottenger 6
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 n Static w Allows direct access w Provides for history sensitive subroutines n Stack-dynamic w Allows recursion w Provides memory savings © 2003 G. Drew Kessler and William M. Pottenger 7
Parameter Passing Methods Semantic modes: n In, out, in out Implementation methods of data transfer: n value, access path (a. k. a. reference) Methods: n Pass-by-value (in) n Pass-by-result (out) w We’re not referring to the subroutine’s return value here! n Pass-by-value-result (in out, value) w (a. k. a. pass-by-copy) n Pass-by-reference (in out, reference) n Pass-by-name (in out, other) © 2003 G. Drew Kessler and William M. Pottenger 8
Parameter Passing Examples Caller Actual parameter Subroutine Formal parameter Pass-by-value Pass-by-result Pass-by-valueresult Pass-by-reference © 2003 G. Drew Kessler and William M. Pottenger 9
Parameter Passing in Various Languages Wide variety of implementations n Fortran: always by reference n Pascal: programmers choice (var) n C: by value (except for arrays/explicit pointers) n C++: by value, C++ ‘reference’ or as with C n Java: ‘built-ins’ by value; others by reference © 2003 G. Drew Kessler and William M. Pottenger 10
Parameter Passing in Ada In, In/Out, Out: n Ada parameter passing can be implemented by value or reference… Ó Morgan Kaufmann (Figure reproduced by permission of author/publisher) © 2003 G. Drew Kessler and William M. Pottenger 11
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) © 2003 G. Drew Kessler and William M. Pottenger 12
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? © 2003 G. Drew Kessler and William M. Pottenger 13
Overloaded subprograms System and user-defined Same name, different operation Right operation identified by parameter and return types n Return type distinction not available if mixed-mode expressions allowed n For example: int f (int i) {…; } float f (int i) {…; } int i = j = 7; float result = f(i) + f(j); // Which f() is called? © 2003 G. Drew Kessler and William M. Pottenger 14
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 <class Type> Type find. Max(Type list[], int size) { Type max = list[0]; for (int i=1; i<size; i++) if (list[i]<max) max = list[i]; return max; } © 2003 G. Drew Kessler and William M. Pottenger 15
Generic Subroutines/Modules Ó Morgan Kaufmann (Figure reproduced by permission of author/publisher) Is it wise to return item by value? © 2003 G. Drew Kessler and William M. Pottenger 16
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 © 2003 G. Drew Kessler and William M. Pottenger 17
Co. Routines © 2003 G. Drew Kessler and William M. Pottenger 18
- Slides: 18