Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Functions g g Data Flow g Scope local global part 4 part 4."— Presentation transcript:

1

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

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

4 Parameter and Data Flow /* in */ 4 pass data into a function/* in */ /* out */ 4 pass data out of a function/* out */ /* inout */ 4 pass data into and out of a function/* inout */

5 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

6 , 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’; } *

7 Data Flow - Example #include using namespace std; void getTemp(double&); void activity(double); void convertCtoF(double&); void main(void) { double temperature; getTemp(temperature); activity(temperature); }

8 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 *

9 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 *

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

11 Scope The section of the program where the variable is valid (known or visible). local = available to only one function or block. Used to limit access. global = available multiple functions or blocks. Used to save call time or reduce complexity of call sequence

12 Scope block Local: The scope of an identifier declared inside a block 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 = { }

13 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 / actually applies to any block * *

14 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?

15 Scope OUTPUT x = 3 before the function call. r = 4 within the function. x = 4 after the function call.

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

17 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 * *

18 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

19 Example Example - ReadOne void ReadOne( /* */ char number, /* */ int& item ) { cout << "Enter value " << number << ": "; cin >> item; return; } * in out

20 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

21 Example Example - Average float Average( /* */ int item1, /* */ int item2, /* */ int item3 ) { int total; total = item1 + item2 + item3; return float(total) / 3; } * in

22 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

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

24 Common Errors 4 Wrong positioning of the called function prototype 4 Terminating a function header with a ; 4 Forgetting the data type of a function’s parameter 4 Remember the NOT rule: 4 Number 4 Order 4 Type


Download ppt "Functions g g Data Flow g Scope local global part 4 part 4."

Similar presentations


Ads by Google