Functions Functions

Full text

(1)

Functions Functions

 Data Flow

 Scope

local global

 Global Resolution Operator

part II part II

(2)

Data Flow Data Flow

Data flow is the direction of the information flow between the function and its caller.

Adding data flow documentation to the function interface is helpful.

The flow could be into a function, out of a

function or both.

(3)

Parameter and Data Flow Parameter and Data Flow

 pass data into a function /* in */ /* in */

 pass data out of a function /* out */ /* out */

 pass data into and out of

a function /* inout */ /* inout */

(4)

Examples

void myFunction( /* in */ double nana, /* in */ int count)

void yourFunction( /* out */ int& num1, /* out */ int& num2) void ourFunction( /* in */ int alpha,

/* inout */ int& beta)

Parameter and Data Flow

Parameter and Data Flow

(5)

Parameter and Data Flow Parameter and Data Flow

 To be certain a function does what you want it to do, write value of variables as you enter and exit a function.

 Put the output statement into a function and call it whenever you need it.

void ShowIt(void)

{ cout<< var1<< ‘\t’ <<var2<< ‘\t’ <<var3<< ‘\n’;

}

*

(6)

Parameter and Data Flow Parameter and Data Flow

Flow In Flow In

int media( /* in */ int cow)

{ cout << “Ten * cow = “ << cow*10;

return (2*cow +5);

}

int media( /* in */ int cow) { cow = 2 * cow;

return (2*cow +5);

}

&

out &

*

(7)

Parameter and Data Flow Parameter and Data Flow

Flow Out Flow Out

void media( /* out */ float& delta, /* out */ float& epsilon )

{

delta = 1.0;

epsilon = 0.0002;

epsilon = epsilon - 0.0001; }

* inout inout

epsilon = epsilon - 0.0001; epsilon = epsilon - 0.0001;

(8)

Parameter and Data Flow Parameter and Data Flow

Flow In and Out Flow In and Out

void update( /* inout */ int& javel,

/* inout */ int & grenelle ) {

javel = 3 * javel;

grenelle++;

}

(9)

Data Flow - Example Data Flow - Example

#include<iostream.h>

void getTemp(double&);

void activity(double);

void convertCtoF(double&);

void main(void) {

double temperature;

getTemp(temperature);

activity(temperature);

}

(10)

Data Flow - Example Data Flow - Example

void getTemp(/* */ double& temp) { {

cout<<"Enter the temperature in degrees C: ";

cin>> temp;

cout<<"The current temperature is "

<<temp<<" degrees celsius."<<endl;

convertCtoF(temp);

} }

void convertCtoF( /* */ double& temp) { {

temp=(1.8)*temp +32;

cout<<"This equals "<<temp

<<" degrees Fahrenheit."<<endl;

} }

out

inout

* *

(11)

Data Flow - Example Data Flow - Example

void activity(/* */ double temp) { {

cout<<"The recommended activity is ";

if(temp>85)

cout<<"swimming."<<endl;

else if(temp>70)

cout<<"tennis."<<endl;

else if(temp>35)

cout<<"golf."<<endl;

else if(temp>10)

cout<<"skiing."<<endl;

else cout<<"dancing."<<endl;

} }

in

*

(12)

Data Flow Data Flow

data flow

data flow parameter-passing parameter-passing for a parameter

for a parameter mechanism mechanism

incoming pass-by-value

outgoing pass-by-reference

incoming/outgoing pass-by-reference

(13)

I/O Example I/O Example

void main(void) {

1

int red, blue;

2

void Mix( int&, int ); // prototype

3

int Blend( int, int ); // prototype

4

red = 5;

5

blue = Blend(3, red + 1);

6

Mix(red, blue);

7

cout << red << ' ' << blue << '\n';

8

Mix(red, blue + red);

9

cout << red << ' ' << blue << '\n';

10

Mix(red, Blend(blue, red));

11

cout << red << ' ' << blue << '\n';

}

(14)

I/O Example I/O Example

M void Mix( int& green, int yellow) {

M1 int purple;

M2 cout << “enter Mix “ << green << ‘ ‘ << yellow << ‘\n’;

M3 purple = green + yellow;

M4 yellow = purple + yellow;

M5 green = purple;

M6 cout << “leav Mix “ << green << ‘ ‘ << yellow << ‘\n’;

}

(15)

I/O Example I/O Example

B int Blend( int red, int green ) {

B1 int yellow;

B2 cout << “enter Blend “ << red <<‘ ‘ << green << ‘\n’;

B3 yellow = red + green;

B4 cout << “leave Blend “ << red <<‘ ‘ << green << ‘\n’;

B5 return (yellow + 1);

}

(16)

void Mix( /* ______ */ int& green, /* ______ */ int yellow )

int Blend( /* ______ */ int red,

/* ______ */ int green )

I/O Example I/O Example

inout in

in in

* * * *

(17)

1 2 3 4 5 B B1 - B5 6

M M1 - M6

I/O Example I/O Example

* * * * 7 8

M M1 - M6 9 10

M B B1 - B5 M1 - M6 11

The lines are executed in this order.

(18)

I/O Example I/O Example

Mix memory Main

green red

blue yellow

purple

purple

(19)

Mix Blend Main

enter Blend 3 6 leave Blend 3 6 enter Mix 5 10 leave Mix 15 25 15 10

enter Mix 15 25 leave Mix 40 65 40 10

enter Blend 10 40 leave Blend 10 40 enter Mix 40 51 leave Mix 91 142 91 10

*

(20)

Scope Scope

A function is like a black box:

You know what goes in and what

comes out, but you do not know what

happens inside the box.

(21)

Scope Scope

The section of the program where the variable is valid (known or visible).

local = available to only one function

global = available several functions

(22)

Scope Scope

Local:

The scope of an identifier declared

inside a block extends from the point of block declaration to the end of that block.

Global:

The scope of an identifier declared outside all functions and classes

extends from the point of declaration to the end of the source file.

* *

(23)

Local Variables Local Variables

 declared within a function definition

 private to a function definition

 variables in different functions are totally independent

 different functions can have variables with the same names; however, each variable will have its own memory

address

* * * *

(24)

int x = 3;

int x = 3; // global because before main void main(void)

{ // no variables local to main( )

void myfunction( ); // prototype

cout <<"x = "<<x<<" before the function call.\n";

myfunction( );

cout <<"x = "<<x<<" after the function call.\n";

}

void myfunction( )

{ int r; // local to myfunction( ) r = ++x;

cout <<"r = "<<r<<" within the function.\n";

}

(25)

Scope Scope

OUTPUT

x = 3 before the function call.

r = 4 within the function.

x = 4 after the function call.

(26)

Example

Example - ReadValues

void main(void)

{ int a, b, c; float avg;

void ReadValues( int&, int&, int& );

void Adjust( int&, int&, int& );

float Average( int, int, int );

void WriteResults( int, int, int, int, float );

ReadValues(a, b, c);

Adjust(a, b, c);

avg = Average(a, b, c);

WriteResults(a, b, c, a + b + c, avg);

}

1.

2.

3.

4.

5.

6.

7.

8.

9.

(27)

Example

Example - ReadValues

1. main declares and calls ReadValues

2. ReadValues declares and calls ReadOne [3x]

3. main declares and calls Adjust 4. main declares and calls Average

5. main declares and calls WriteResults

* * * *

(28)

Example

Example - ReadValues

void ReadValues( /* */ int& x, /* */ int& y, /* */ int& z )

{

void ReadOne( char, int& );

ReadOne('1', x );

ReadOne('2', y );

ReadOne('3', z );

return;

}

* *

out out

out

(29)

Example

Example - ReadOne

void ReadOne( /* */ char number, /* */ int& item )

{

cout << "Enter value " << number << ": ";

cin >> item;

return;

}

* *

in

out

(30)

Example

Example - Adjust

void Adjust( /* */ int& i, /* */ int& j, /* */ int& k )

{

int smallest;

smallest = i;

if (j < smallest) i = i - smallest;

smallest = j; j = j - smallest;

if (k < smallest) k = k - smallest;

smallest = k; return;

}

* *

inout inout

inout

(31)

Example

Example - Average

float Average( /* */ int item1, /* */ int item2, /* */ int item3 )

{

int total;

total = item1 + item2 + item3;

return float(total) / 3;

}

* *

in in

in

(32)

Example

Example - WriteResults

void WriteResults( /* */ int item1,

/* */ int item2, /* */ int item3, /* */ int total, /* */ float average ) {

cout << "Adjusted values: " << item1 << ", "

<< item2 << ", " << item3 << '\n' << "Sum: " << total

<< " Average: " << average << '\n';

return;

}

* *

in

in in in

in

(33)

ReadValues Adjust Average WriteResults

ReadOne

Main

Enter value 1: 23 Enter value 2: 56 Enter value 3: 78

Adjusted values: 0, 33, 55

Sum: 88 Average: 29.3333

(34)

void swap(int, int); // a global function global void main(void)

{ int x = 5, y = 10;

1.

cout <<“Main-before swap, x: “<<x<<" y: "<<y<< '\n';

swap(x, y);

2.

cout <<"Main-after swap, x: "<<x<<" y: "<<y<<'\n';

}

void swap(int x, int y)

{ int temp;

3.

cout <<"Swap-before swap, x: "<<x<<" y: "<<y<<'\n';

temp = x;

temp = x; x = y; x = y; y = temp; y = temp;

4.

cout <<"Swap-after swap, x: "<<x<<" y: "<<y<<'\n';

}

(35)

Scope Scope

OUTPUT

1. Main-before swap: x: 5 y: 10

3. Swap-before swap: x: 5 y: 10

4. Swap-after swap: x: 10 y: 10 5 5

2. Main-after swap: x: 5 y: 10

(36)

void Block1(int, char &);

void Block2( );

int a1; // global

char a2; // global

int main() { . . .

}

Scope within a Block Scope within a Block

slide

slide 1 1 of of 22

(37)

void Block1(int a1, char &b2) // prevents access { // to global a1

int c1; // local to Block1 int d1; // local to Block1 }

} }

slide

slide 2 2 of of 2 2

*

. . .

void Block2()

{ int a1; // prevents access to global a1

int b1; // local to Block2

while (…)

{ // Block3 // Block3

int c1; // local to Block3

int b2; // prevents non-local access

} } . . . // to b2 in Block1

(38)

Scope within a block - Ex. 1 Scope within a block - Ex. 1

predict the output predict the output

void scope2(void); // function prototype void main(void)

{ int v=100; int v=100;

cout <<"v BEFORE function = "<<v<<'\n';

scope2();

cout <<"v AFTER function = "<<v<<'\n';

}

slide

slide 1 1 of of 22

(39)

Scope within a block - Ex. 1 Scope within a block - Ex. 1

predict the output predict the output

1. void scope2(void) //function header

2. { double v = 5.5;

3 . int k, j;

4. cout << "v outside block = " << v<<'\n';

5. for (k=1; k<=3; k++)

6. { int v = 17; // initialized in function

7. for (j=1; j<=2; j++)

8. { v++;

9. cout << "v inside block = " << v<<'\n';

10. }

11. }

12. cout << "v outside block = " << v<<'\n';

13. }

slide

slide 2 2 of of 22

(40)

Scope within a block - Ex. 2 Scope within a block - Ex. 2

void scope2(void); // function prototype void main(void)

{ int v=100; int v=100;

cout <<"v BEFORE function = "<<v<<'\n';

scope2();

cout <<"v AFTER function = "<<v<<'\n';

}

slide

slide 1 1 of of 22

(41)

Scope within a block - Ex. 2 Scope within a block - Ex. 2

slide

slide 2 2 of of 22

1. void scope2(void) //function header

2. { double v = 5.5;

3. int k, j;

4. cout << "v outside block = " << v <<'\n';

5. for (k=1; k<=3; k++)

6. { int v = 17; int v = 17 // initialized in function

7. for (j=1; j<=2; j++)

8. { v ++; v

9. cout << "v inside block = " << v <<'\n'; v

10. }

11. }

12. cout << "v outside block = " << v <<'\n';

}

(42)

Scope within a block Scope within a block

OUTPUT

100 BEFORE 5.5 outside 18 18 inside inside 19 19 inside inside 18 18 inside inside 19 19 inside inside 18 18 inside inside 19 19 inside inside 5.5 outside 100 AFTER

j lo op s j lo op s

} } }

*

} k loop

(43)

Global Resolution Operator Global Resolution Operator :: ::

double rougon = 999.99; // global void main(void)

{ double rougon = 12.3; // local

cout<< rougon << “ = rougon, local\n”

cout<< ::rougon << “ = rougon, global\n”; ::

}

OUTPUT OUTPUT

12.3 = rougon, local

999.99 = rougon, global

* *

(44)

Variable Storage Classes Variable Storage Classes

Local auto static

register  while (



) { int k = 1;

k++;



}

* * * while (



)

{ static int k = 1;

k++;



}

(45)

Global

static

extern 

Variable Storage Classes

Variable Storage Classes

(46)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

int x = 1; // global variable main()

{ int x = 5; // local variable to main

cout << "local x in outer scope of main is " << x << endl;

{ // start new scope int x = 7;

cout << "local x in inner scope of main is " <<x<< endl;

} // end new scope

cout << "local x in outer scope of main is " << x << endl;

(47)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

(continued)

a(); // a has automatic local x b(); // b has static local x

c(); // c uses global x

a(); // a reinitializes automatic local x

b(); // static local x retains its previous value c(); // global x also retains its value

cout << "local x in main is " << x << endl;

return 0;

} // end of main()

(48)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

void a(void)

{ int x = 25; // initialized each time a is called

cout << endl << "local x in a is " << x << " after entering a" << endl;

++x;

cout << "local x in a is " << x

<< " before exiting a" << endl;

}

(49)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

void b(void)

{ static int x = 50; // Static initialization only // first time b is called cout << endl << "local static x is " << x

<< " on entering b" << endl;

++x;

cout << "local static x is " << x << " on exiting b" << endl;

}

(50)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

void c(void)

{ cout << endl << "global x is " << x << " on entering c" << endl;

x *= 10;

cout << "global x is " << x << " on exiting c" << endl;

}

(51)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

a(); // a has automatic local x b(); // b has static local x

c(); // c uses global x

a(); // a reinitializes automatic local x

b(); // static local x retains its previous value c(); // global x also retains its value

a();

b();

c();

*

(52)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

OUTPUT

local x in outer scope of main is 5 local x in inner scope of main is 7 local x in outer scope of main is 5 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c

global x is 10 on exiting c

(53)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c

global x is 100 on exiting c

local x in main is 5

(54)

Scope

Scope & & Storage Classes - an example Storage Classes - an example

void a(void)

{ int x = 25; // initialized each time a is called

cout << endl << "local x in a is " << x << " after entering a" << endl;

++x;

cout << "local x in a is " << x

<< " before exiting a" << endl;

}

<< ‘\t’ <<

<< ‘\t’ << :: :: x << endl; x

*

(55)

Common Errors Common Errors

 Passing incorrect data types Passing incorrect data types

 Using the same variable name for Using the same variable name for different variables

different variables

ex. local - in both the calling and ex. local - in both the calling and

called functions called functions global - must use

global - must use :: ::

(56)

Common Errors Common Errors

 Wrong positioning of the called Wrong positioning of the called function prototype

function prototype

 Terminating a function header Terminating a function header with a

with a ; ;

 Forgetting the data type of a Forgetting the data type of a function’s parameter

function’s parameter

(57)

Debugging Debugging

char = junk;

char = junk; and and cin << junk; cin << junk;

Prevention - plan first!

Prevention - plan first!

Valuation tables Valuation tables

Display values Display values

Use Use Find Find or or Replace Replace for for == == vs. vs. = = C++ Debugger

C++ Debugger

(58)

“Heavier-than-air flying

machines are impossible.”

Lord Kelvin,

President of the

Royal Society, 1895

End Note 1

End Note 1

(59)

“If A = success, then the formula is A = X + Y + Z.

X is work. Y is play.

Z is keep your mouth shut.”

Albert Einstein

End Note 2

End Note 2

Figure

Updating...

References

Updating...

Related subjects :