Parameter PassingIn this set of notes you will learn about:
|
Note: These notes cover Section 8.3 of the textbook |
Parameter Passing
int fac(int n) { return n ? n*fac(n-1) : 1; } |
Parameter Passing Modes in C
int fac(int n) { if (n < 0) n = 1; return n ? n*fac(n-1) : 1; } swap(int *a, int *b) { int t = *a; *a = *b; *b = t; } where a and b are integer pointer formal parameters, and *a and *b in the function body dereference the pointers to access the integers |
Parameter Passing Modes in Fortran
SUBROUTINE SHIFT(A, B, C) INTEGER A, B, C A = B B = C END |
Parameter Passing Modes in Pascal
procedure swap(var a:integer, var b:integer) var t; begin t := a; a := b; b := t end where the var formal parameters a and b are passed by reference (var t declares a local variable) |
Parameter Passing Modes in C++
swap(int &a, int &b) { int t = a; a = b; b = t; } where the formal parameters a and b are passed by reference, e.g. swap(x, y) exchanges integer variables x and y store_record_in_file(const huge_record &r) { ... } |
Parameter Passing Modes in Languages With Reference Model of Variables
|
Parameter Passing Modes in Java
|
Parameter Passing Modes in Ada
procedure shift(a:out integer, b:in out integer, c:in integer) is begin a := b; b := c; end shift; where a is passed out, b is passed in and out, and c is passed in |
Parameter Passing Modes in Ada (cont'd)
{ int tmpa, tmpb = *b, tmpc = c; // copy input values before start tmpa = tmpb; tmpb = tmpc; *a = tmpa; *b = tmpb; // copy result values before return } |
Parameter Aliasing Problems
int i, &j = i; //j refers to i (is an alias for i) ... i = 2; j = 3; cout << i; // prints3 shift(int &a, int &b, int &c) { a = b; b = c; } The result of shift(x, y, x) is that x is set to y but y is unchanged int sum = 0; score(int &total, int val) { sum += val; total += val; } The result of score(sum, 7) is that sum is incremented by 14 |
Call by Name Parameter Passing
#define max(a,b) ( (a)>(b) ? (a) : (b) ) max(somefunc(),0) results in the evaluation of somefunc() twice if it returns a value >0 |
Call by Name Parameter Passing in Algol 60
real procedure sum(expr, i, low, high); value low, high; low and high are passed by value real expr; expr and i are passed by name integer i, low, high; begin real rtn; rtn := 0; for i := low step 1 until high do rtn := rtn + expr; the value of expr depends on the value of i sum := rtn return value by assigning to function name end sum
|
Parameter Passing Problems
procedure swap(a, b) integer a, b, t; begin t := a; a := b; b := t end swap Consider swap(i, a[i]), which executes t := i i := a[i] this changes i a[i] := t assigns t to wrong array element procedure shift(a:outinteger, b:in out integer, c:ininteger) is begin a := b; b := c; end shift; |
Conformant Array Parameters
function sum(A : array [low..high : integer] of real) : real ... Function sum accepts real typed arrays and low and high act like formal parameters that get the lower and upper bound index of the actual array parameter at run time |
Closures as Parameters
procedure apply_to_A(function f(n:integer):integer; var A : array [low..high : integer] of integer); var i:integer; begin for i := low to high do A[i] := f(A[i]) end void apply_to_A(int (*f)(int), int A[], int A_size) { int i; for (i=0; i<A_size; i++) A[i]=f(A[i]); } The int (*f)(int) is a formal parameter that is a pointer to a function f from int to int |
Default Parameters
void print_num(int n, int base = 10) ... A call to print_num(35) uses default value 10 for base as if print_num(35,10) was called procedure put(item : in integer; width : in field := default_width; base : in number_base := 10) is ... A call to put(35) uses default values for the width and base parameters |
Positional Versus Named Parameters
put(item => 35, base => 8); this "assigns" 35 to item and 8 to base, which is the same as: put(base => 8, item => 35); and we can mix positional and name parameters as well: put(35, base => 8); |
Variable Number of Arguments
#include <stdarg.h> int plus(int num, ...) { int sum; va_list args; // declare list of arguments va_start(args, num); // initialize list of arguments for (int i=0; i<num; i++) sum += va_arg(args, int); // get next argument (must be int) va_end(args); // clean up list of arguments return sum; } Function plus adds a bunch of integers together, where the number of arguments is the first parameter to the function, e.g. plus(4,3,2,1,4) returns 10 |
Function Returns
function max(a : integer; b : integer) : integer; begin if a>b then max := a else max := b end There is no return statement, instead the function returns with the value of max when it reaches end int max(int a, int b) { if (a>b) return a; else return b; } int fac(int n) { int rtn=1; for (i=2; i<=n; i++) rtn*=i; return rtn; } |