Top Banner
Functions Functions Data Flow Scope local global part 4 part 4
23

Functions g g Data Flow g Scope local global part 4 part 4.

Dec 28, 2015

Download

Documents

Justina Oliver
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Functions g g Data Flow g Scope local global part 4 part 4.

FunctionsFunctions Data Flow Scope

local global

part 4part 4

Page 2: Functions g g Data Flow g Scope local global part 4 part 4.

Data FlowData 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.

Page 3: Functions g g Data Flow g Scope local global part 4 part 4.

Parameter and Data FlowParameter 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 */

Page 4: Functions g g Data Flow g Scope local global part 4 part 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 FlowParameter and Data Flow

Page 5: Functions g g Data Flow g Scope local global part 4 part 4.

Parameter and Data FlowParameter 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’;}

*

Page 6: Functions g g Data Flow g Scope local global part 4 part 4.

Data Flow - ExampleData Flow - Example#include<iostream>using namespace std;void getTemp(double&);void activity(double);void convertCtoF(double&);

void main(void){

double temperature;getTemp(temperature);activity(temperature);

}

Page 7: Functions g g Data Flow g Scope local global part 4 part 4.

Data Flow - ExampleData Flow - Examplevoid 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

* *

Page 8: Functions g g Data Flow g Scope local global part 4 part 4.

Data Flow - ExampleData Flow - Examplevoid 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

*

Page 9: Functions g g Data Flow g Scope local global part 4 part 4.

ScopeScope

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.

Page 10: Functions g g Data Flow g Scope local global part 4 part 4.

ScopeScopeThe section of the program where the variable is valid

(known or visible).

local = available to only one functionor block. Used to limit access.

global = available multiple functions or blocks. Used to save call time or reduce complexity of call sequence

Page 11: Functions g g Data Flow g Scope local global part 4 part 4.

ScopeScope

Local:The scope of an identifier declared inside a blockblock extends from the point of 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.

* *

Block = { }

Page 12: Functions g g Data Flow g Scope local global part 4 part 4.

Local VariablesLocal 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

actually applies to any block

* * * *

Page 13: Functions g g Data Flow g Scope local global part 4 part 4.

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

}

// what happens to global x?

Page 14: Functions g g Data Flow g Scope local global part 4 part 4.

ScopeScope

OUTPUTx = 3 before the function call.r = 4 within the function. x = 4 after the function call.

Page 15: Functions g g Data Flow g Scope local global part 4 part 4.

Example Example - ReadValuesvoid 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.

Page 16: Functions g g Data Flow g Scope local global part 4 part 4.

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

* * * *

Page 17: Functions g g Data Flow g Scope local global part 4 part 4.

Example Example - ReadValuesvoid ReadValues( /* */ int& x, /* */ int& y,

/* */ int& z ){ void ReadOne( char, int& );

ReadOne('1', x ); ReadOne('2', y ); ReadOne('3', z );

return;}

* *

outout

out

Page 18: Functions g g Data Flow g Scope local global part 4 part 4.

Example Example - ReadOne

void ReadOne( /* */ char number,

/* */ int& item )

{

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

cin >> item;

return;

}

* *

in

out

Page 19: Functions g g Data Flow g Scope local global part 4 part 4.

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;

}

* *

inoutinout

inout

Page 20: Functions g g Data Flow g Scope local global part 4 part 4.

Example Example - Average

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

/* */ int item3 )

{

int total;

total = item1 + item2 + item3;

return float(total) / 3;

}

* *

in in

in

Page 21: Functions g g Data Flow g Scope local global part 4 part 4.

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

ininin

in

Page 22: Functions g g Data Flow g Scope local global part 4 part 4.

ReadValues Adjust Average WriteResults

ReadOne

Main

Enter value 1: 23Enter value 2: 56Enter value 3: 78Adjusted values: 0, 33, 55Sum: 88 Average: 29.3333

Page 23: Functions g g Data Flow g Scope local global part 4 part 4.

Common ErrorsCommon Errors Wrong positioning of the called function Wrong positioning of the called function

prototypeprototype

Terminating a function header with a Terminating a function header with a ;; Forgetting the data type of a function’s Forgetting the data type of a function’s

parameterparameter Remember the NOT rule:Remember the NOT rule:

NNumberumber OOrderrder TTypeype