cs 1400 chap 6. functions general form; type name ( parameters ) { … return value ; } parameters...

25
CS 1400 Chap 6

Post on 19-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

CS 1400

Chap 6

Page 2: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Functions

• General form;

type Name ( parameters ){ …

return value ;}

parameters is a list of comma-separated declarations

Page 3: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

More on functions…

• Functions can be placed– after boilerplate but before main() simplest– after main() covered later– in another file covered later

• A function has its own variables!

Page 4: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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.

Page 5: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Value passing…int MyFunc (float a, int b)

{ int temp = x+y;

return temp;

}

int main()

{ float x=5.1;

int z, y=21;

z = MyFunc (x, y);

Page 6: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Value passing diagrams…main()

MyFunc()

z x y

a b

5.1 21

temp

MyFunc (x, y);

5.1 21

Page 7: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Examples:

• Function ExampleSquare cout << Square (21.6);

Cube y = Cube (z);

Max cout << Max (a, b);

Abs y = Abs (x);

PrintHead PrintHead();

GetBetween y = GetBetween (21, 80);

Round cout << Round (3.567);

Page 8: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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

Page 9: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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 1MyFunc(); // outputs 2…

Page 10: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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

Page 11: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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

5 9 12 39

Page 12: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Results…

main()

Swap()

x y cost rate

a b

see x in main()

see y in main()

temp

5 9 12 399 5

5

Page 13: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

main()

Swap()

x y cost rate

a b

see cost in main()

see rate in main()

temp

Swap (cost, rate);

9 5 12 39

Page 14: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

main()

Swap()

x y cost rate

a b

see cost in main()

see rate in main()

temp

9 5 12 39

Results…

39 12

12

Page 15: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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).

Page 16: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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

Page 17: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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;

}

Page 18: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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

Page 19: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Example A

void Swap (int &a, int &b){ int temp = a; a = b; b = temp;}

int main(){ … // function Swap() is used here return 0;}

Page 20: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Example Bvoid Swap (int &a, int &b); // function prototype

int main(){ … // function Swap() is used here return 0;}

void Swap (int &a, int &b){ int temp = a; a = b; b = temp;}

Page 21: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

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;

}

Page 22: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Overloading functions…

• Several functions may have the same name

• Each function must have a unique signature– name– number and type of parameters

Note that the return type of a function is not part of its signature!

Page 23: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Default Arguments

• A function may specify default values for parameters in its prototype.

• Example:

• Example function calls:

int TotalPrice (float item_cost; float taxrate = 0.065; int count=1);

cout << TotalPrice (5.98);

cout << TotalPrice (7.95, 0.075);

cout << TotalPrice (3.56, 0.055, 3);

Page 24: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Default Arguments…

• Only the last or ending arguments may have default values

• Illegal:

cout << TotalCost (4.68, , 4);

Page 25: CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations

Reference Parameters (bis)

• All arguments for reference parameters must be simple variables – expressions are not allowed;

int MyFunc (int x, int& y);

z = MyFunc (5+a, 7*b);

OK Illegal!