Title: Functions Parameters
1Functions Parameters Variable Scope
Chapter 6
2Overview
- Using Function Arguments and Parameters
- Differences between Value Parameters and
Reference Parameters - Using Local Variables in a Function
- Variable Scope and Duration
3Functions
- 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.
4Weve 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)
5When 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!
6Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
7Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
8Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
9Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
10Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
11Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
12Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
13Example 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
14GetRoots prototype
- 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
15Function 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 lt 0) return false else
root1 (-b sqrt(temp))/(2.0 a)
root2 (-b - sqrt(temp))/(2.0 a)
return true
16- include ltiostreamgt
- include ltcmathgt
- bool GetRoots(float, float, float, float,
float) - using namespace std
-
- int main ( )
- float c1, c2, c3, first, second
- int count 0
- ...
- while ( count lt 5 )
- cin gtgt c1 gtgt c2 gtgt c3
- if (GetRoots(c1, c2, c3, first, second))
- cout ltlt first ltlt second ltlt endl
- else
- cout ltlt "No real solution"
- count
-
- ...
17Data Flow ? Passing Mechanism
18Functions 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
19void GetGrade(char letter) // prototype
- void GetGrade(char letter)
-
- cout ltlt Enter employee rating. ltlt endl
- cout ltlt Use A, B, C, D, F
- cin gtgt letter
- while ((letter ! 'B') (letter ! 'C')
- (letter ! 'A') (letter ! 'D')
- (letter ! 'F'))
-
- cout ltlt "Invalid. Enter again "
- cin gtgt letter
-
20Using a Parameter Passing Mechanism
- 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