Passing by Value - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Passing by Value

Description:

cout system('Pause'); Actual Parameters. 4. Passing by Value ... cout system('Pause'); return 0; 12. Modular Program Age. void read_year( int& birth_year) ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 14
Provided by: mlo3
Category:
Tags: passing | pause | value

less

Transcript and Presenter's Notes

Title: Passing by Value


1
Passing by Value
  • Passing by Value
  • Formal Parameters, place holders in the header of
    the function
  • Actual Parameters, parameters contained in the
    function call

2
Passing by Value
  • void print_nums (int num1, int num2, int num3)
  • cout ltlt endl
  • cout ltlt Number 1 is ltlt num1 ltlt endl
  • cout ltlt Number 2 is ltlt num2 ltlt endl
  • cout ltlt Number 3 is ltlt num3 ltlt endl

3
Passing by Value
  • int main( )
  • number_1 10
  • number_2 20
  • number_3 30
  • cout ltlt The Numbers are ltlt endl
  • print_nums ( number_1, number_2, number_3 )
  • cout ltlt system(Pause)

4
Passing by Value
  • void print_nums ( int num1, int num2, int num3)

Number_1 10 Number_2 20 Number_3
30 print_nums ( number_1, number_2, number_3
) cout ltlt system(Pause)
5
Passing by Value
  • The Numbers are
  • Number 1 is 10
  • Number 2 is 20
  • Number 3 is 30

Screen
6
Passing by Reference
  • Passing by Reference
  • Variable Parameters, place holders in the header
    of the function
  • Actual Parameters, parameters contained in the
    function call

7
Passing by Reference
  • void print_nums ( int num1, int num2, int
    num3)
  • cout ltlt endl
  • cout ltlt Number 1 is ltlt num1 ltlt endl
  • cout ltlt Number 2 is ltlt num2 ltlt endl
  • num3 num1 num2
  • cout ltlt Number 3 is ltlt num3 ltlt endl

8
Passing by Reference
  • int main( )
  • number_1 10
  • number_2 20
  • number_3 number_1 number_2
  • cout ltlt The Numbers are ltlt endl
  • cout ltlt number_1 ltlt number_2 ltlt number_3) ltlt
    endl
  • print_nums ( number_1, number_2, number_3 )
  • cout ltlt Number 3 in main is ltlt number3 ltlt
    endl
  • cout ltlt system(Pause)

9
Passing by Reference
  • void print_nums ( int num1, int num2, int num3)

number_1 10 number_2 20 number_3
number_1 number_2 print_nums ( number_1,
number_2, number_3 )
address
400
404
408
10
Passing by Reference
  • The Numbers are 10 20 30
  • Number 1 is 10
  • Number 2 is 20
  • Number 3 is 200
  • Number 3 in main is 200

Screen
11
Modular Program Age
  • include ltiostreamgt
  • include ltiomanipgt
  • include ltstringgt
  • using namespace std
  • void read_year( int)
  • void compute_age (int, int)
  • void display_age (int age)
  • int main( )
  • int birth_year, age
  • read_year(birth_year)
  • compute_age(age, birth_year)
  • display_age(age)
  • cout ltlt system(Pause)
  • return 0

12
Modular Program Age
  • //
  • void read_year( int birth_year)
  • cout ltlt endl
  • cout ltlt Enter year of birth ltlt endl
  • cin gtgt birth_year
  • cout ltlt endl
  • //
  • void compute_age (int age, int birth_year)
  • const current_year 2005
  • age current_year birth_year

13
Modular Program Age
  • //
  • void display_age (int age)
  • cout ltlt endl
  • cout ltlt Your age after this years birthday is
    ltlt age ltlt . ltlt endl
  • cout ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com