functions g g data flow g scope local global part 4 part 4
TRANSCRIPT
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.
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 */
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
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’;}
*
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);
}
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
* *
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
*
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.
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
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 = { }
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
* * * *
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?
ScopeScope
OUTPUTx = 3 before the function call.r = 4 within the function. x = 4 after the function call.
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.
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
* * * *
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
Example Example - ReadOne
void ReadOne( /* */ char number,
/* */ int& item )
{
cout << "Enter value " << number << ": ";
cin >> item;
return;
}
* *
in
out
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
Example Example - Average
float Average( /* */ int item1, /* */ int item2,
/* */ int item3 )
{
int total;
total = item1 + item2 + item3;
return float(total) / 3;
}
* *
in in
in
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
ReadValues Adjust Average WriteResults
ReadOne
Main
Enter value 1: 23Enter value 2: 56Enter value 3: 78Adjusted values: 0, 33, 55Sum: 88 Average: 29.3333
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