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

18
© 2003 G. Drew Kessler an d William M. Pottenger 1 Subroutines (Part 1) CSE 262, Spring 2003

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

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

© 2003 G. Drew Kessler and William M. Pottenger

1

Subroutines (Part 1)

CSE 262, Spring 2003

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

© 2003 G. Drew Kessler and William M. Pottenger 2

SubroutinesMethod 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

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

© 2003 G. Drew Kessler and William M. Pottenger 3

Design Issues & MechanismsMethod of parameter passingFunction vs. procedure Mathematical function vs. program

statementLocal and non-local referencing environmentsOverloaded subroutinesGeneric subroutinesSeparate compilation, independent compilation Require info for type checking separately

compiled subroutines?Aliasing and side effect problems

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

© 2003 G. Drew Kessler and William M. Pottenger 4

Subroutine ParametersParameters 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

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

© 2003 G. Drew Kessler and William M. Pottenger 5

Subprogram DefinitionSubprogram 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 parametersParameter protocol: profile plus subroutine return typeSubprogram declaration or prototype: provides info needed by compiler to do type-checking, before definition (some languages need forward qualifier)

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

© 2003 G. Drew Kessler and William M. Pottenger 6

Parameter DesignsAssociation between formal and actual parameters: Positional parameters

void sort(int list[], int size, bool ascending);sort(numbers, n, true); // call to sort()

Keyword parameterssort(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?

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

© 2003 G. Drew Kessler and William M. Pottenger 7

Local and Non-Local Referencing Environments

Local variables defined inside subroutineFormal parameters are usually local variablesNon-local variables are either global or are variables that are in scope but local to another subroutineLocal variable storage Static

Allows direct access Provides for history sensitive subroutines

Stack-dynamic Allows recursion Provides memory savings

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

© 2003 G. Drew Kessler and William M. Pottenger 8

Parameter Passing MethodsSemantic modes:

In, out, in outImplementation 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)

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

© 2003 G. Drew Kessler and William M. Pottenger 9

Parameter Passing ExamplesCaller Subroutine

Pass-by-value

Pass-by-result

Pass-by-value-result

Pass-by-reference

Actual parameter Formal parameter

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

© 2003 G. Drew Kessler and William M. Pottenger 10

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

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

© 2003 G. Drew Kessler and William M. Pottenger 11

Parameter Passing in Ada

In, In/Out, Out: Ada parameter

passing can beimplementedby value orreference…

Morgan Kaufmann (Figure reproduced by permission of author/publisher)

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

© 2003 G. Drew Kessler and William M. Pottenger 12

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 + akend

Use:x := sum (i, 1, n, V[i])

x := sum (i, 1, m, sum(j, 1, n, A[i, j]))

(Jensen’s Device)

n

iiVx

1

m

i

n

jjiAx

1 1,

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

© 2003 G. Drew Kessler and William M. Pottenger 13

Call-by-Name ProblemConsider 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]:=8swap(i, A[i])

Do other methods have a problem with this?

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

© 2003 G. Drew Kessler and William M. Pottenger 14

Overloaded subprogramsSystem and user-definedSame name, different operationRight 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?

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

© 2003 G. Drew Kessler and William M. Pottenger 15

Generic subroutinesProvide routine, where parameter and/or variable types are defined at a later time.New instance of routine with specific type created when needed or explicitlyAda generic unitsC++ templatestemplate <class Type>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;

}

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

© 2003 G. Drew Kessler and William M. Pottenger 16

Generic Subroutines/Modules Morgan Kaufmann (Figure reproduced by permission of author/publisher)

Is it wise to return item by value?

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

© 2003 G. Drew Kessler and William M. Pottenger 17

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

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

© 2003 G. Drew Kessler and William M. Pottenger 18

Co- Routines