Functions Parameters & Variable Scope Chapter 6. 2 Overview Using Function Arguments and Parameters...
Transcript of Functions Parameters & Variable Scope Chapter 6. 2 Overview Using Function Arguments and Parameters...
Functions Parameters &Variable Scope
Chapter 6
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
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
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
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
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
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
Anatomy of a function call
0
0
-234535
1243
0
0
25
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
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
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
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
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
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
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
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
#include <iostream>
#include <cmath>
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
Data Flow Passing Mechanism
Direction Passing mechanism
Input parameter pass-by-value
Output parameter pass-by-reference
Both pass-by-reference
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
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
Pass-by-Reference use only if the design of the called function requires that
it be able to modify the value of the parameterPass-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