Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1400 23 Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }

Similar presentations


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

1 CS 1400 23 Feb 2007 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 Examples: FunctionExample Squarecout << Square (21.6); Cubey = Cube (z); Maxcout << Max (a, b); Absy = Abs (x); PrintHeadPrintHead(); GetBetweeny = GetBetween (21, 80); Roundcout << Round (3.567);

6 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; …

7 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…

8 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);

9 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

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

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

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

13 Example: Max() The function Max() is intended to determine the maximum value of a list of N numbers input by the user (The argument N is provided by the caller).

14 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

15 Functions need not return a value! If a function does not return a value, its type is void and it does not need a return void MyFunc ( int a ) { cout << “the value is: “ << a; cout << “and the square is: “ << a*a; }

16 Alternate function locations… Functions can be placed below calling functions if the function prototype is above the other functions that call it. A prototype is the first line or title of a function – followed by a semicolon. Functions can be placed in a separate file if the function prototype is above other functions that call it and the function file is included in the project

17 Example A void Swap (int &a, int &b) { int temp = a; a = b; b = temp; } int main() { …// function Swap() is used here return 0; }

18 Example B void Swap (int &a, int &b); int main() { … // function Swap() is used here return 0; } void Swap (int &a, int &b) { int temp = a; a = b; b = temp; }

19 Example C FILE A.cpp: void Swap (int &a, int &b); int main() { … // Swap used here return 0; } FILE B.cpp: void Swap (int &a, int &b) { int temp = a; a = b; b = temp; }


Download ppt "CS 1400 23 Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }"

Similar presentations


Ads by Google