Functions II It just wont leave - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

Functions II It just wont leave

Description:

We have discussed value-return and void functions ... precede the variable name with an ampersand (&) in the function definition. ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 53
Provided by: jason102
Category:

less

Transcript and Presenter's Notes

Title: Functions II It just wont leave


1
Chapter 9,10
  • Functions II It just wont leave!!!

2
Functions
  • We have discussed value-return and void functions
  • The last type of function we will cover is a type
    of void function that data can be sent back and
    forth

3
Void Functions
  • In the void function, the data that is being sent
    is stored in the parameter of the function.
  • void print_value(int i)
  • The parameter is in between the open and closed
    ().
  • Parameters
  • Value Parameters
  • Reference Parameters

4
Void Functions
  • There are two types of parameters
  • Value Parameters
  • Reference Parameters

5
Value Parameters
  • When you pass a variable to a function by value,
    a copy of the value in the variable is given to
    the function for it to use.
  • If the variable is changed within the function,
    the original copy of the variable in the calling
    function remains the same.

6
Value Parameters Example
7
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

8
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

9
Comment lines and Libraries
10
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

11
Declaring a Function
  • void print_value(int i)
  • Remember If a function starts with anything
    other then the word void, it is a value returning
    fuction.
  • In this function, a variable called i with the
    data type int is being created.
  • The variable i is being sent from the main
    function to the print_value function

12
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

13
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

14
Declaring the i variable
15
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

16
(No Transcript)
17
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

18
Function call
19
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

20
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

21
(No Transcript)
22
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

23
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

24
(No Transcript)
25
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt "The value after the function exits is
    " ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

26
(No Transcript)
27
Remember
  • In a value parameter, only a copy of the variable
    is being sent. The original value of i is not
    effected in the main function

28
  • // passval.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt "The value before the function is " ltlt
    i ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt "The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltlt i ltltendl

29
End of the program
30
Reference Parameters
  • Functions that pass variables by reference will
    pass any changes you make to the variables back
    to the calling function.
  • To pass a variable by reference, simply precede
    the variable name with an ampersand () in the
    function definition.

31
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

32
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

33
Comment lines and Libraries
34
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

35
Declaring a Function
  • void print_value(int i)
  • Remember If a function starts with anything
    other then the word void, it is a value returning
    fuction.
  • In this function, a variable called i with the
    data type int is being created.
  • The variable i is being sent from the main
    function to the print_value function

36
  • Tells the compiler that the parameter is a
    reference parameter

37
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

38
Main Function
39
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

40
Declaring the i variable
41
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

42
(No Transcript)
43
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

44
Function call
45
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

46
(No Transcript)
47
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

48
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

49
(No Transcript)
50
  • // passref.cpp
  • include ltiostreamgt
  • using namespace std
  • void print_value(int i) // function prototype
  • int main()
  • int i 2
  • cout ltlt"The value before the function is " ltlt i
    ltlt endl
  • print_value(i)
  • cout ltlt"The value after the function exits is "
    ltlt i ltlt endl
  • return 0
  • void print_value(int i)
  • cout ltlt"The value passed to the function is "
    ltlt i ltlt endl
  • i i 2 // the value in the variable i is
    doubled
  • cout ltlt"The value at the end of the function is
    "ltltiltltendl

51
(No Transcript)
52
Remember
  • In a reference parameter, any changes to the i
    variable will change the variable in the main
    function
Write a Comment
User Comments (0)
About PowerShow.com