Subprograms subroutines, procedures, functions, methods.
-
Upload
logan-mason -
Category
Documents
-
view
237 -
download
1
Transcript of Subprograms subroutines, procedures, functions, methods.
![Page 1: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/1.jpg)
Subprograms
subroutines, procedures, functions, methods
![Page 2: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/2.jpg)
Vocabulary
float average(float list[], int n) { … }
av = average(marks,20);
call
header / prototype
protocolformal parametersactual(arguments)
parameter profile
![Page 3: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/3.jpg)
Functions vs Procedures
procedures are statements - do not return a value
Collections.sort(marks, 20);
functions are expressions - return a value
double x = 1000.0 + Math.exp(10.0);
in c, c++, Java, all methods are functions; can be used as procedures; ‘void’s must be used as procedures
![Page 4: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/4.jpg)
Parameters
keyword
positional
call
call - return
return
pass-by-
•value
•reference
•nameexample Java:
•all parameters are positional
•primitive parameters are call, pass by value
•object parameters are call-return, pass by reference
![Page 5: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/5.jpg)
Keyword parameter passing
order of actual parameters can be changed if each parameter is identified by keyword (FORTRAN, Ada)
av = average(n=>20,list=>marks)
![Page 6: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/6.jpg)
Direction of data passing
call - data goes into subprogram only at beginning of execution
return - data goes from subprogram back to caller at end of execution
call-return - data passes in both directions: into subroutine at call and back to caller at end of execution
![Page 7: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/7.jpg)
Direction of data passing
call - protects caller’s data from effects of subprogram
return - useful for initializing data in caller; alternative route for returning data
call-return - gives subprogram access to change caller data
caller subprogram
caller subprogram
caller subprogram
![Page 8: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/8.jpg)
Implementing parameters (1) Pass-by-value
pass-by-value (call) pass-by-result (return) pass-by-value-result (call - return)
Data is copied between actual and formal parametersa = 10 proc sub(int x)sub (a) {print a print x
x = 1000 print x}
a x
![Page 9: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/9.jpg)
Implementing parameters (1) Pass-by-value
pass-by-value (call) pass-by-result (return) pass-by-value-result (call - return)
What happens when actual parameters are not variables? (constants or expressions)
![Page 10: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/10.jpg)
Implementing parameters (1) Pass by value, result, value-result
a = 10 proc sub(int x)sub (a) {print a print x
x = 1000 print x}
a x
stack sub
![Page 11: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/11.jpg)
Implementing parameters (2) Pass-by-reference (call - return OR call if
actual parameters are made read-only) Formal parameter refers to same
memory cell as actual parameter
a = 10 proc sub(int x)sub (a) {print a print x
x = 1000 print x}
a x
![Page 12: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/12.jpg)
Implementing parameters (2) Pass-by-reference
a = 10 proc sub(int x)sub (a) {print a print x
x = 1000 print x}
a x
stack sub
address of a
![Page 13: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/13.jpg)
Implementing parameters (2)
Pass-by-reference Formal parameter refers to same memory
cell as actual parameter What happens when actual parameters are
not variables? (constants or expressions)java example jButton.addActionListener(new ButtonListener());
![Page 14: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/14.jpg)
Implementing parameters (3) Pass-by-name formal is bound to actual only when
accessed or assigned (i.e., not at call) effect depends on form of actual parameter:
constant - same as pass-by-value variable - same as pass-by-reference expression - like reference but reference may
change
![Page 15: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/15.jpg)
Implementing parameters (3) - pass-by-name example
procedure BIGSUB p. 364 Sebesta integer GLOBAL, LIST[1:2]; procedure SUB(PARAM) begin PARAM := 3; GLOBAL := GLOBAL + 1; // note scope! PARAM := 5; end;begin LIST[1] := 2; LIST[2] := 2; GLOBAL := 1; SUB(LIST[GLOBAL]);end;
![Page 16: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/16.jpg)
Implementing Parameters (3)
Pass-by-name
x
j=4
x
thunk
sub(x)j=2sub(list[j])
list[j]
jlist
![Page 17: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/17.jpg)
Parameters - design issues (1)
type checking no checking - mismatched types ->
reinterpretation of data type checking - control of type match or
legal coercion
![Page 18: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/18.jpg)
Parameters - design issues (2)
array parameters - for 2 or more dimensions, length of 2nd, 3rd, etc dimensions is needed to computer offset
tradeoff compile-time (efficient) offset expression
forces fixed array dimensions call-time (less efficient) offset expression
handles arrays of any size (java)
![Page 19: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/19.jpg)
Parameters - design issues (3) security / efficiency tradeoff
security: design according to minimum access requirements: call , return, call-return
efficiency: reference is time and space efficient for large data objects but implies call-return
compromises allow programmer choice ‘read-only’ references (C++) what does java offer?
![Page 20: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/20.jpg)
Passing procedures as parameters example of procedure passingprocedure m() var integer[3] arr = {1,2,3}; procedure s (integer[3] k) begin print (k[1]+k[2]+k[3]); end procedure sub (integer[3] cc, procedure w) begin w(cc); endbegin sub(arr, s);end
![Page 21: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/21.jpg)
Issues unique to procedure parameters
Does protocol of actual parameter procedure match protocol of formal?
1. - early Pascal unchecked2. - later Pascal – parameters in formal
procedure parameter3. - c pointers to procedures are
passed; their type identifies protocol
![Page 22: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/22.jpg)
Function design extreme definition: NO SIDE-EFFECTS
no access to global variables only call parameters only effect through return value
return values: single primitive value – math model single value including reference/pointer any one value including record, array or
procedure
![Page 23: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/23.jpg)
Coroutines – preview to concurrency
subroutines in symmetric control model (not caller-callee relationship)
goal: virtual parallelism Simula – process simulation java – threads classic example – large file copy
read and write coroutines with shared buffer
![Page 24: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/24.jpg)
Sharing data space
global identifiers by scoping rules instructions to compiler to share
space (Fortran COMMON) explicit specification of external units
needed (Fortran 90, java)
![Page 25: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/25.jpg)
Reusability with subprograms
OVERLOADING: reusing a subprogram name in different scopes with different protocols (like operators*) conflict with default parameters
POLYMORPHIC: different parameter types in different activations dynamic typing type templates C++
*C++ allows overloading of operators
![Page 26: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/26.jpg)
C++ templatestemplate <class Etype>void Swap (Etype & First, Etype & Second){ Etype Temp = First; First = Second; Second = Temp;}... in calling programint X = 5, Y = 6, Z = 7;double A = 3.5, B = 9.4;Swap(X,Y); //instantiate int versionSwap(X,Z); //re-use int versionSwap(A,B); //instantiate double versionSwap(A,Z); // illegal – type mismatch
![Page 27: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/27.jpg)
Reusability and compilation problem: variables and procedures
defined in one unit and accessed in another – type checking
1. separate compilation (Ada) compile time check of compatibility
2. independent compilation (c) no type checking – run time type errors
3. no compilation except complete programs (original Fortran II)
![Page 28: Subprograms subroutines, procedures, functions, methods.](https://reader033.fdocuments.in/reader033/viewer/2022051018/56649f0d5503460f94c21f18/html5/thumbnails/28.jpg)
Separate Compilation
compilation checks library of exported entities for types and protocols of external references
functionint max(int,int)
unit A
A’s compilation unitint max(int,int)
library
i = max(j,k)
unit Bcompile A
compile B
?