Data - This too shall pass Thanks Dr. Mullins - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Data - This too shall pass Thanks Dr. Mullins

Description:

A copy of a value is passed from the calling function to ... cout 'Hypotenuse = ' Pythagorus(height, base) endl; double Pythagorus(double a, double b) ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 8
Provided by: norman95
Category:

less

Transcript and Presenter's Notes

Title: Data - This too shall pass Thanks Dr. Mullins


1
Data - This too shall pass Thanks Dr. Mullins
  • passing by reference may give back a
    value Chapter 4


2
Passing Data - by Value
  • passing by valueA copy of a value is passed
    from the calling function to the called function.

Can use as temp vars
double Pythagorus(double a, double b) a
a a b b b double c sqrt(a
b) return c
double Pythagorus(double a, double
b) double c c sqrt(aa bb) return
c
9.0
16.0
3.0
4.0

3
Storing Values into Parameters
firstnum 865 secnum9090find_max(firstnum,
secnum)
find_max(x, y)
x
y
865
9090

4
Passing Data - by Value
  • double Pythagorus(double, double)
  • void main(void)
  • double height 4.0, base 3.0
  • cout ltlt Hypotenuse ltlt Pythagorus(height,
    base)ltltendl
  • . . .
  • double Pythagorus(double a, double b)
  • double c c sqrt(aa bb)
  • return c


5
Passing Data - by Value
  • double Pythagorus(double a, double b)
  • double c
  • a
  • b
  • c sqrt(aa bb)
  • return c

c
3.0
6.4
4.0
4.0
5.0
4.03.0
back in main cout ltlt height cout ltlt base

6
Passing Data by-value Example
  • void print_val(int) // function prototype
  • void main(void)
  • int w 3
  • cout ltlt"w before the function call is
    "ltltwltlt\n
  • print_val(w)
  • cout ltlt"w after the function call is
    "ltltwltlt\n
  • void print_val(int q)
  • coutltlt"Value passed to the function is
    "ltltqltltendl
  • q q 2 // doubles the value
  • coutltlt"Value at the end of the function is "ltlt q
    ltltendl

7
Passing Data by-value Results
  • Output
  • w before the function call 3
  • Value passed to the function is 3
  • Value at the end of the function is 6
  • w after the function call is 3
Write a Comment
User Comments (0)
About PowerShow.com