Functions - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Functions

Description:

total = item1 item2 item3; return float(total) / 3; in. in. in. Example - WriteResults ... int total, /* */ float average ) cout 'Adjusted values: ' item1 ... – PowerPoint PPT presentation

Number of Views:13
Avg rating:3.0/5.0
Slides: 24
Provided by: norman81
Category:
Tags: functions | item1

less

Transcript and Presenter's Notes

Title: Functions


1
Functions
  • Data Flow
  • Scope local global
  • part 4

2
Data Flow
  • Data flow is the direction of the information
    flow between the function and its caller.
  • Adding data flow documentation to the function
    interface is helpful.
  • The flow could be into a function, out of a
    function or both.

3
Parameter and Data Flow
  • pass data into a function / in /
  • pass data out of a function / out /
  • pass data into and out of a function / inout
    /

4
Parameter and Data Flow
  • Examples
  • void myFunction( / in / double nana,
    / in / int count)
  • void yourFunction( / out / int num1,
    / out / int num2)
  • void ourFunction( / in / int alpha,
    / inout / int beta)

5
Parameter and Data Flow
  • To be certain a function does what you want it to
    do, write value of variables as you enter and
    exit a function.
  • Put the output statement into a function and call
    it whenever you need it.

void ShowIt(void) coutltlt var1ltlt \t
ltltvar2ltlt \t ltltvar3ltlt \n

6
Data Flow - Example
  • includeltiostreamgt
  • using namespace std
  • void getTemp(double)
  • void activity(double)
  • void convertCtoF(double)
  • void main(void)
  • double temperature
  • getTemp(temperature)
  • activity(temperature)

7
Data Flow - Example
out
  • void getTemp(/ / double temp)
  • coutltlt"Enter the temperature in degrees C "
  • cingtgt temp
  • coutltlt"The current temperature is "
  • ltlttempltlt" degrees celsius."ltltendl
  • convertCtoF(temp)
  • void convertCtoF( / / double temp)
  • temp(1.8)temp 32
  • coutltlt"This equals "ltlttemp
  • ltlt" degrees Fahrenheit."ltltendl

inout

8
Data Flow - Example
in
  • void activity(/ / double temp)
  • coutltlt"The recommended activity is "
  • if(tempgt85)
  • coutltlt"swimming."ltltendl
  • else if(tempgt70)
  • coutltlt"tennis."ltltendl
  • else if(tempgt35)
  • coutltlt"golf."ltltendl
  • else if(tempgt10)
  • coutltlt"skiing."ltltendl
  • else
  • coutltlt"dancing."ltltendl


9
Scope
  • A function is like a black boxYou know what
    goes in and what comes out, but you do not know
    what happens inside the box.

10
Scope
  • The section of the program where the variable is
    valid (known or visible).
  • local available to only one functionor block.
    Used to limit access.
  • global available multiple functions or blocks.
    Used to save call time or reduce complexity of
    call sequence

11
Scope
Block
  • LocalThe scope of an identifier declared inside
    a block extends from the point of declaration to
    the end of that block.
  • GlobalThe scope of an identifier declared
    outside all functions and classes extends from
    the point of declaration to the end of the source
    file.


12
Local Variables
  • declared within a function definition
  • private to a function definition
  • variables in different functions are totally
    independent
  • different functions can have variables with the
    same names however, each variable will have its
    own memory address
  • actually applies to any block


13
  • int x 3 // global because before main
  • void main(void)
  • // no variables local to main( )
  • void myfunction( ) // prototype
  • cout ltlt"x "ltltxltlt" before the function
    call.\n"
  • myfunction( )
  • cout ltlt"x "ltltxltlt" after the function
    call.\n"
  • void myfunction( )
  • int r // local to myfunction( )
  • r x
  • cout ltlt"r "ltltrltlt" within the function.\n"

// what happens to global x?
14
Scope
  • OUTPUT
  • x 3 before the function call.
  • r 4 within the function.
  • x 4 after the function call.

15
Example - ReadValues
1. 2. 3. 4. 5. 6. 7. 8. 9.
  • void main(void)
  • int a, b, c float avg
  • void ReadValues( int, int, int )
  • void Adjust( int, int, int )
  • float Average( int, int, int )
  • void WriteResults( int, int, int, int, float
    )
  • ReadValues(a, b, c)
  • Adjust(a, b, c)
  • avg Average(a, b, c)
  • WriteResults(a, b, c, a b c, avg)

16
Example - ReadValues
  • 1. main declares and calls ReadValues
  • 2. ReadValues declares and calls ReadOne 3x
  • 3. main declares and calls Adjust
  • 4. main declares and calls Average
  • 5. main declares and calls WriteResults


17
Example - ReadValues
  • void ReadValues( / / int x, /
    / int y,
  • / / int z )
  • void ReadOne( char, int )
  • ReadOne('1', x )
  • ReadOne('2', y )
  • ReadOne('3', z )
  • return

out

18
Example - ReadOne
  • void ReadOne( / / char number,
  • / / int item )
  • cout ltlt "Enter value " ltlt number ltlt " "
  • cin gtgt item
  • return

in
out

19
Example - Adjust
inout
  • void Adjust( / / int i, /
    / int j,
  • / / int k )
  • int smallest
  • smallest i
  • if (j lt smallest) i i - smallest
  • smallest j j j - smallest
  • if (k lt smallest) k k - smallest
  • smallest k return


20
Example - Average
  • float Average( / / int item1, / / int
    item2,
  • / / int item3 )
  • int total
  • total item1 item2 item3
  • return float(total) / 3

in

21
Example - WriteResults
  • void WriteResults( / / int item1,
  • / / int item2, /
    / int item3,
  • / / int total, /
    / float average )
  • cout ltlt "Adjusted values " ltlt item1 ltlt ", "
  • ltlt item2 ltlt ", " ltlt item3 ltlt '\n'
  • ltlt "Sum " ltlt total
  • ltlt " Average " ltlt average ltlt '\n'
  • return

in

22
Main
ReadValues
Adjust
Average
WriteResults
Enter value 1 23 Enter value 2 56 Enter value
3 78 Adjusted values 0, 33, 55 Sum 88
Average 29.3333
ReadOne
23
Common Errors
  • Wrong positioning of the called function
    prototype
  • Terminating a function header with a
  • Forgetting the data type of a functions
    parameter
  • Remember the NOT rule
  • Number
  • Order
  • Type
Write a Comment
User Comments (0)
About PowerShow.com