Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 4 Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }

Similar presentations


Presentation on theme: "CS 1400 4 Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }"— Presentation transcript:

1 CS 1400 4 Oct 2006 Chap 6

2 Functions General form; type Name ( parameters ) { … return value ; }

3 types… The return value of a function must match the function type. Arguments passed to function parameters must match in type and in position. if either of these rules is neglected, the computer will force (cast) a match.

4 value passing… int MyFunc (float x, char y) { … return 55; } int main() { int z; z = MyFunc (3.14, ‘F’);

5 Global variables… Variables declared outside of main() or a function are globally accessible. int x; int main() { cout << x; … Local variables may eclipse global variables int x; int main() { int x; cout << x; …

6 Static variables… Static variables retain their values when a function exits and are only created and initialized once void MyFunc () { static int counter = 0; cout << counter++; } int main() {MyFunc();// outputs 0 MyFunc();// outputs 1 MyFunc();// outputs 2…

7 Reference parameters… A function using reference parameters can modify corresponding arguments void Swap (int &a, int &b) { int temp = a; a = b; b = temp; } int main() { … Swap (x, y); Swap (cost, rate);

8 The value passed or copied into a reference parameter is a forwarding address… main() Swap() x y cost rate a b see x in main() see y in main() temp Swap (x, y); 591239

9 Results… main() Swap() x y cost rate a b see x in main() see y in main() temp 591239 95 5

10 main() Swap() x y cost rate a b see cost in main() see rate in main() temp Swap (cost, rate); 951239

11 main() Swap() x y cost rate a b see cost in main() see rate in main() temp 951239 Results… 3912

12 Example: MaxMin() The function MaxMin() is intended to determine two values; the maximum and minimum values of a list of 10 numbers input by the user

13 Quiz Suppose the library function abs() was not available… Write a possible substitution for this function that could be added to your program. Example segment using abs() cout << abs(x); cout << abs(y-x);

14 Linear Search Search array list with size elements: find the position of variable value. bool found = false; int position = -1; for (int n=0; n<size && !found; n++) { if (list[n] == value) { position = n; found = true; }

15 Passing arrays as parameters… Array parameters are declared with empty brackets; int MyFunction (float array[ ]); Usually, a function with an array parameter will need to know the size of the array or the number of elements to be considered; int MyFunction2 (float array[ ], int size); Array parameters are always reference parameters!

16 Linear Search as a function… int LinearSearch (int list[], int size, int value) { bool found = false; int position = -1; for (int n=0; n<size && !found; n++) { if (list[n] == value) { position = n; found = true; } return position; }


Download ppt "CS 1400 4 Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }"

Similar presentations


Ads by Google