10
Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

Embed Size (px)

Citation preview

Page 1: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

Comp 255: Lab 02Parameter Passing

pass by value pass by reference

In, out, in/out and array parameters

Page 2: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

int readInt()// reads/inputs unsigned integer{ char str[80]; int i; int number;

i = 0; str[i] = getchar();A1: if (str[i] == '\n') goto A2; i++; str[i] = getchar(); goto A1;A2: str[i] = '\0';

// convert to integer via Horner's Method number = 0; i = 0;A3: if (str[i] == '\0') goto A4; number = number * 10 + (str[i] - 48); i++; goto A3;A4: return(number);}

Page 3: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

int printInt(int number)// prints unsigned integer{ char str[80]; int i;

i = 0;C1: str[i] = (number % 10) + 48; number = number / 10; i++; if (number > 0)// bottom test loop goto C1;

// print out string backwardsC3: if (i == 0) goto C4; i--; putchar(str[i]); goto C3;C4: return(0);}

Page 4: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

int printStr(char s[]) // prints cstring{ int i = 0;P1: if (s[i] == '\0') goto P2; putchar(s[i]); i++; goto P1;P2: return(0);}

Page 5: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

Pass By Reference in C

In pass by reference the address of a variable is passed; not the value of a variable

In the definition of the function this is indicated by prefixing a *, the pointer operator, to the formal parameter

int fubar(int *x, int *y)

So in the call of the function you must explicitly pass the address of the variable using the & operator

err = fubar(&a, &b)

Page 6: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

Pass by Reference in C

In the definition of the function you must dereference all pass by reference parameters to obtain the value of the parameter using the * dereference operator.

int fubar(int *x, int *y){ int q, r; if (*y == 0) goto F1; q = *x / *y; r = *x % *y; *x = q; *y = r; return(0);F1: return(1); // error!}

Page 7: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

max01 function examplepass by value; function returns a value

int max01(int a, int b){ if (a > b) return a; else return b;}

int main(){ int a, b, c;

c = max01(a, b); // function call

Function definition

Page 8: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

max02 function example

int max02(int a, int b, int *c){ if (a > b) c* = a; else c* = b;}

int main(){ int a, b, c;

max02(a, b, &c); //function call

Function definition

Page 9: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

Four Logical Types of Parameters

in parameters: used to pass values into a function; call by value

out parameters: used to return values from a function; call by reference

in/out parameters: parameters will are passed in to a function, modified and returned to the calling program; call by reference

array parameters: parameter is an array type; syntactically pass by value

Page 10: Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

Notes