Sebesta Ch.9 Lecture

advertisement
Sebesta Ch.9 Lecture
By Dr. Milica Barjaktarovic
Subprograms
 Programs in their own way, but executable only by being
called from another code
 Provide a way to organize a large program into **reusable** chunks
Assumptions:
 Each sub has a single entry point
 The calling unit is suspended while the sub is active, i.e.
until the sub returns
 Control is returned to the caller when the sub returns
(There are some exceptions – concurrent code, Fortran
multientry, etc.)
Subs have to be defined. The sub header (i.e. sub name and the
list of parameters) has input-output parameters called formal
parameters (i.e. dummy variables).
Some languages also have sub declarations i.e. prototypes (e.g.
C/C++).
A sub is called from another program using actual parameters.
Examples:
def adder(parameters) in Python
void adder(parameters) in C
procedure adder (parameters) in Ada
subroutine added(parameters) in FORTRAN
Subprograms can be either procedures or functions.
Procedures can return many things and change the input
parameters.
Functions are really special case of procedures, meant to
mimic paper-n-pencil math and calculate “only one thing”
and not change the inputs. In practice, this is not always the
case.
Questions to ask regarding subs:
 Are local variables statically or dynamically allocated?
 Can subprograms be nested (i.e. one sub contains definitions
for others)
a. If yes, what is the referencing environment of a passed
sub
 What parameter-passing methods are used
 Are the types of actual parameters checked against the
formal parameters
 Can subs be overloaded
a. polymorphysim
 Can subs be generic
Questions specifically for functions:
 Are side effects allowed?
 What types of values can be returned?
Passing the actual parameters:
Calling programs can access a sub via non-local variables that the
sub can “see”; or by passing parameters.
Sub is typically called with the actual parameters in the same
order as the formal parameters (called positional parameters); OR
some languages have keyword parameters, where the programmer
specifies the order.
Example in Python:
def
adder(length, list, sum)
definition
adder(length = my_length, list=my_array, sum=my_sum)
call
In some languages, like Python, C++, it is possible to have
“default values” for some parameters, e.g.:
def adder(list, sum, length = 10)
adder(my_array, my_sum)
Parameters can be passed from the caller either as one-way or
two-ways:
1. only into the sub (i.e. IN),
2. only from the sub (i.e. OUT) or
3. in/out from the sub (i.e. INOUT).
Example in FORTRAN:
PROGRAM polynomial
INTEGER :: y, x, k, z
INTEGER :: n, a(999), k, c
PRINT *, "This program calculates “
PRINT *, “y = SUM[k=1,n]a(k)*(x^k)"
CALL read_input(n)
CALL poly_calc(a,n,y,x,c)
END PROGRAM polynomial
SUBROUTINE read_input(n)
INTEGER, INTENT(OUT) :: n
PRINT *, "Please enter the values for n”
READ *, n
END SUBROUTINE read_input
SUBROUTINE poly_calc(a, n, y, x, c)
INTEGER, INTENT(IN) :: n
INTEGER, INTENT(OUT) :: y, A(999)
INTEGER :: k
….
END SUBROUTINE poly_calc
Subprograms can be either procedures or functions.
Example in FORTRAN:
FORTRAN subroutines list both input and output variables in the input parameters.
Functions list only input variables, since function name is the output variable that is
passed back to the main program.
Both of these subporograms calculate n!. One is subroutine, and another is a
function.
Subroutine lists input and output variables in the list of input parameters:
subroutine factorial (n, fact)
integer, INTENT (IN): n
integer, INTENT (OUT): fact
!input variable, has to be passed to
!the subroutine by the main program
!output variable, will be made
!passed back to the main program
when the subroutine is finished
Function returns only one item, the variable that is also the name of the function.
integer fact(n)
integer, INTENT(IN): n
!fact is the output variable and will
!be passed back to the main
!program; notice that it has to be
!declared in the main program
!input variable
The entire code is in:
http://www2.hawaii.edu/~milica/mat190/testfactorial.f90
Way of passing from caller:
1. only into the sub (i.e. IN)
2. only from the sub (i.e. OUT)
Implementation:
pass-by-value
pass-by-result
3. in/out from the sub (i.e. INOUT) pass-by-value-result
(also called pass-by-copy)
or
pass by reference
or pass-by-name (e.g.
assembly language)
Pass-by-value:
 typically copy the input parameter
o Great for scalars
o Terrible for arrays
 Alternative: use the access path – but then the original input
variable has to be write protected – tough
Pass-by-result:
 Can accidentally cause parameter collision and change the
output parameters depending on the order of execution, e.g.
o Adder(a, a)
Pass-by-reference:
 Can be slower, for scalars
 Much faster for arrays
 Can cause aliasing by mistake
TYPICALLY IMPLEMENTED USING THE RUN-TIME
STACK.
Fig.9.2 in the book.
Passing in the major languages:
http://www2.hawaii.edu/%7Enreed/ics313/lectures/10subprog.pdf
page 5.
Example in C:
void fun( int &first, int &second);
void swap1(int a, int b);
void swap2(int *a, int *b);
main() {
int c = 3;
int d = 4;
fun(total, total);
swap1(c, d);
swap2(&c, &d);
}
void swap1(int a, int b) {
int temp = a;
a = b;
b = temp;
}
In C:
void swap2(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
In C++:
void swap2(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}
Generic Functions
The main idea: write a template and instantiate it later with
specific data types.
Examples in C++
Template <class Type>
Type max(Type first, Type second) {
return first > second ? first : second;
}
int max(int first, int second) {
return first > second ? first : second;
}
// either implicit or explicit instantiation
void main() {
int a,b,c;
char d,e,f;
int x, y, z;
… assign values to all variables ….
c = max(a, b);
f = max(d, e);
z = max( x++, y);
}
Is this equivalent to calling a macro:
#define maxmacro(a, b) ((a) > (b)) ? (a) : (b))
z = maxmacro(x++, y);
Examples in Java 5.0
Generic parameters must be classes, and can be limited to
certain classes. There can be many instantiations but only one
copy of the code.
public static <T> T SomeFun(T[] somearray) { … }
public static <T extends Comparable> T
BoundedSomeFun(T[] somearray) { …}
SomeFun<int>(myarray);
Download