Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.

Similar presentations


Presentation on theme: "Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference."— Presentation transcript:

1 Functions Parameters & Variable Scope Chapter 6

2 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference Parameters  Using Local Variables in a Function  Variable Scope and Duration

3 3 Functions  Every C++ program must have a main function  execution starts by looking for a “main”  All other functions are called directly from main, or indirectly through a chain of function called from main  Function Calls  One function calls another by using the name of the called function next to ( ) enclosing an argument list. ex. strlen(FirstName)  A function call temporarily transfers control from the calling function to the called function.

4 4 We’ve been using value parameters  Simple types (like int, char, float, double) are value parameters by default  you can specify reference parameters with &  To create a reference parameter place an & after the type in both the function heading and prototype void Cube(int &x);

5 5 When to Use Reference Parameters  If you want your function to  Assign a value to a variable from where it was called  Change the value of a variable permanently  Using a reference parameter  is like giving someone the key to your home  the key can be used by the other person to change the contents of your home!

6 6 Anatomy of a function call 0 0 -234535 1243 0 0 25 -234534 0 0 0 101 4000 4004 4008 4012 4016 4020 4024 4028 4032 4036 4040 4044 age myByRef(int& a) { a = 10; } myByVal(int a) { a = 100; } int main() { int age = 25; myByVal(age); myByRef(age);

7 7 Anatomy of a function call 0 0 -234535 1243 0 0 25 -234534 0 0 0 101 4000 4004 4008 4012 4016 4020 4024 4028 4032 4036 4040 4044 age myByRef(int& a) { a = 10; } myByVal(int a) { a = 100; } int main() { int age = 25; myByVal(age); myByRef(age);

8 8 Anatomy of a function call 0 0 -234535 1243 0 0 25 0 0 0 101 4000 4004 4008 4012 4016 4020 4024 4028 4032 4036 4040 4044 age myByRef(int& a) { a = 10; } myByVal(int a) { a = 100; } int main() { int age = 25; myByVal(age); myByRef(age); a

9 9 Anatomy of a function call 0 0 -234535 1243 0 0 25 100 0 0 0 101 4000 4004 4008 4012 4016 4020 4024 4028 4032 4036 4040 4044 age myByRef(int& a) { a = 10; } myByVal(int a) { a = 100; } int main() { int age = 25; myByVal(age); myByRef(age); a

10 10 Anatomy of a function call 0 0 -234535 1243 0 0 25 100 0 0 0 101 4000 4004 4008 4012 4016 4020 4024 4028 4032 4036 4040 4044 age myByRef(int& a) { a = 10; } myByVal(int a) { a = 100; } int main() { int age = 25; myByVal(age); myByRef(age);

11 11 Anatomy of a function call 0 0 -234535 1243 0 0 25 4024 0 0 0 101 4000 4004 4008 4012 4016 4020 4024 4028 4032 4036 4040 4044 age myByRef(int& a) { a = 10; } myByVal(int a) { a = 100; } int main() { int age = 25; myByVal(age); myByRef(age); &a

12 12 Anatomy of a function call 0 0 -234535 1243 0 0 10 4024 0 0 0 101 4000 4004 4008 4012 4016 4020 4024 4028 4032 4036 4040 4044 age myByRef(int& a) { a = 10; } myByVal(int a) { a = 100; } int main() { int age = 25; myByVal(age); myByRef(age); &a

13 13 Example: Pass-by-Reference  We want to find 2 real roots for a quadratic equation with coefficients a,b,c. Write a prototype for a bool function named GetRoots( ) with 5 parameters.  Return value is true if real roots exist, false otherwise  The first 3 (value) parameters are type float. The last 2 are reference parameters of type float. Need to pass 2 values back…

14 14 bool GetRoots(float, float, float, float&, float&);  This function uses 3 incoming values  It calculates 2 outgoing values, returning the 2 real roots of the quadratic equation with coefficients a, b, c GetRoots prototype

15 15 Function Definition bool GetRoots( float a, float b, float c, float& root1, float& root2) { float temp; // local variable temp = b * b - 4.0 * a * c; if (temp < 0) return false; else { root1 = (-b + sqrt(temp))/(2.0 * a); root2 = (-b - sqrt(temp))/(2.0 * a); return true; } }

16 16 #include bool GetRoots(float, float, float, float&, float&); using namespace std; int main ( ) { float c1, c2, c3, first, second; int count = 0;... while ( count < 5 ){ cin >> c1 >> c2 >> c3; if (GetRoots(c1, c2, c3, first, second)) cout << first << second << endl; else cout << "No real solution"; count++ ; }... }

17 17 Data Flow  Passing Mechanism DirectionPassing mechanism Input parameterpass-by-value Output parameterpass-by-reference Bothpass-by-reference

18 18 Functions with reference parameters  Write a prototype and definition for a void function called GetGrade( ) with one reference parameter of type char  The function repeatedly prompts the user to enter a character at the keyboard until one of these has been entered: A, B, C, D, F

19 19 void GetGrade(char& letter); // prototype void GetGrade(char& letter) { cout << “Enter employee rating.” << endl; cout << “Use A, B, C, D, F : ” ; cin >> letter; while ((letter != 'B') && (letter != 'C') && (letter != 'A') && (letter != 'D') && (letter != 'F')) { cout << "Invalid. Enter again: "; cin >> letter; }

20 20  Pass-by-Reference  use only if the design of the called function requires that it be able to modify the value of the parameter  Pass-by-Constant-Reference  use if the called function has no need to modify the value of the parameter, but the parameter is very large  guarantees that the called function cannot modify the variable passed in  Pass-by-Value  use when none of the reasons given above apply Using a Parameter Passing Mechanism


Download ppt "Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference."

Similar presentations


Ads by Google