Functions Parameters - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Functions Parameters

Description:

Variable Scope. Chapter 6. 2. Overview. Using Function Arguments and ... guarantees that the called function cannot modify the variable passed in. Pass-by-Value ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 21
Provided by: sylvi150
Category:

less

Transcript and Presenter's Notes

Title: Functions Parameters


1
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
Weve 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
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
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
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
9
Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
10
Anatomy of a function call
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
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
12
Anatomy of a function call
myByRef(int a) a 10 myByVal(int a) a
100 int main() int age 25
myByVal(age) myByRef(age)
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
GetRoots 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

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

17
Data Flow ? Passing Mechanism
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 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

20
Using 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
Write a Comment
User Comments (0)
About PowerShow.com