functions ----------- pass by value pass by reference ic 210

26
Functions ----------- Pass by Value Pass by Reference IC 210

Post on 21-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Functions ----------- Pass by Value Pass by Reference IC 210

Functions-----------

Pass by ValuePass by Reference

IC 210

Page 2: Functions ----------- Pass by Value Pass by Reference IC 210

Arguments vs. ParametersArguments:

• function call

• actual values passed to function

Parameters:

• Placeholder in function heading waiting for arguments

answer = cube (x);

double cube (double inputVal)

Argument (function call)

parameter (function heading)

Page 3: Functions ----------- Pass by Value Pass by Reference IC 210

What is the output?

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

??5

Call by Value

Page 4: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

Call by value: “Sending a copy”

5

DoubleIt

?

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 5: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

Call by value: “Sending a copy”

5

?

?DoubleIt

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 6: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

Call by value: “Sending a copy”

5

?

?DoubleIt

int DoubleIt (int);int main ( ){ int x = 5;

results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

y ?

Page 7: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

y

Call by value: “Sending a copy”

5

?

?

?X’s value is copied into Y

DoubleIt

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 8: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

y

Call by value: “Sending a copy”

5

?

?

5

DoubleIt

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 9: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

y

Call by value: “Sending a copy”

5

5

?

10

DoubleIt ?

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 10: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

y

Call by value: “Sending a copy”

10DoubleIt

10

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

5

?

Page 11: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

y

Call by value: “Sending a copy”

5

?

DoubleIt 10

10

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 12: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

Call by value: “Sending a copy”

5

10

DoubleIt 10

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 13: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

Call by value: “Sending a copy”

5

10

DoubleIt ?

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 14: Functions ----------- Pass by Value Pass by Reference IC 210

X

results

Call by value: “Sending a copy”

5

DoubleIt ?

10

int DoubleIt (int);int main ( ){ int x = 5;

int results = DoubleIt (x);cout << x;return 1;

}int DoubleIt (int y)} y = 2*y;return y;}

Page 15: Functions ----------- Pass by Value Pass by Reference IC 210

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y) // Function Definition{ int z = y;

y = 2 * y;return y + z;}

What is the output?

??14,21

Call by

Reference

Page 16: Functions ----------- Pass by Value Pass by Reference IC 210

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y) // Function Definition{ int z = y;

y = 2 * y;return y + z;}

Call by reference: “Getting the original”

TripleIt

x 7

Page 17: Functions ----------- Pass by Value Pass by Reference IC 210

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y) // Function Definition{ int z = y;

y = 2 * y;return y + z;}

results

Call by reference: “Getting the original”

TripleIt

?

x 7

Page 18: Functions ----------- Pass by Value Pass by Reference IC 210

results ?

?TripleIt

Call by reference: “Getting the original”

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y){ int z = y;

y = 2 * y;return y + z;}

x 7

Page 19: Functions ----------- Pass by Value Pass by Reference IC 210

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y){ int z = y;

y = 2 * y;return y + z;}

results ?

?TripleIt

Call by reference: “Getting the original”

x 7

Page 20: Functions ----------- Pass by Value Pass by Reference IC 210

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y) { int z = y;

y = 2 * y;return y + z;}

results

y, x

?

?

X’s address is referenced

TripleIt

Call by reference: “Getting the original”

7

Page 21: Functions ----------- Pass by Value Pass by Reference IC 210

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y){ int z = y;

y = 2 * y;return y + z;}

results

y, x

?

TripleIt ?

Call by reference: “Getting the original”

z 7

7

Page 22: Functions ----------- Pass by Value Pass by Reference IC 210

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y) { int z = y;

y = 2 * y;return y + z;}

results

y, x

?

TripleIt ?

Call by reference: “Getting the original”

z 7

714

Page 23: Functions ----------- Pass by Value Pass by Reference IC 210

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y){ int z = y;

y = 2 * y;return y + z;}

results

y, x

?TripleIt

14

?

Call by reference: “Getting the original”

z 7

21

Page 24: Functions ----------- Pass by Value Pass by Reference IC 210

results

y, x

?

TripleIt 21

14

Call by reference: “Getting the original”

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y){ int z = y;

y = 2 * y;return y + z;}

z 7

_

Page 25: Functions ----------- Pass by Value Pass by Reference IC 210

results 21

TripleIt 21

Call by reference: “Getting the original”

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y){ int z = y;

y = 2 * y;return y + z;}

x 14

Page 26: Functions ----------- Pass by Value Pass by Reference IC 210

results 21

TripleIt ?

Call by reference: “Getting the original”

int TripleIt (int&); // Prototypeint main ( ) // Function Definition{ int x = 7;

int results = TripleIt (x);cout << x << “,“ << results;return 1;

}int TripleIt (int& y){ int z = y;

y = 2 * y;return y + z;}

x 14