Professional Documents
Culture Documents
(1)
Objectives
Understand design issues in subprograms: parameter passing type checking overloading and parameterized procedures
(2)
Parameters
Parameters allow the subprogram to manipulate data external to the subprogram. Provide local names for external data. Allow binding of local names to different data during each call.
Alternative: non-local variable referencing. Also allows access to external data. Reduces readability of subprogram. Reduces flexibility of programming -Requires changing external state before passing
(4)
(5)
(7)
(8)
void swap (int *px, int *py) { int z; z = *px; *px = *py; *py = z; } main () { int a = 1; int b = 2; swap (&a, &b); return a; }
Call-by-reference: Swap.c
(11)
Swap is called with the address of locations. Swap is declared to accept pointers.
pseudocode foo (x,y) { i = y; } pseudo main () { int i = 2; int j = 3; foo (i, j); return i; }
Call-by-value-result
What does this program return? What would this program return under callby-value or call-by-reference?
(12)
Call-by-name
Uses actual textual substitution of arguments for parameters
Not used in modern programming languages due to great potential for name conflicts Example: the call swap (i, A[i]) would rewrite the body of swap before execution as: void swap (int x, int y) { int z; z = i; i = A[i]; A[i] = z; } What is the problem here?
(13)
(14)
The formal parameter should specify type of subprogram for type checking (just like it specifies the type of the variable). Subprogram type is its protocol. Example: Pascal -procedure int(function fun (x:real) :real); begin funval := fun(2.5) end; Example: C++ -Only pointers can be passed. -Pointer contains protocol information.
(15)
(16)
(17)
Compilation
Large system development must avoid total recompilation whenever possible.
Separate compilation: Compilation units can be compiled at different times, but recompilation is not independent if there is any use of one module by another. Requires access to other units during compilation. Independent compilation: Units can be compiled without access to others. (18) Errors are caught at link-time or run-time.
End of module
(19)