COMP 102 Programming Fundamentals I - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

COMP 102 Programming Fundamentals I

Description:

Pass-by-value. Operation. By using additional storage for the parameters ... Pass-by-reference. Advantages: No extra storage and data copying is needed ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 15
Provided by: typey73
Category:

less

Transcript and Presenter's Notes

Title: COMP 102 Programming Fundamentals I


1
COMP 102Programming Fundamentals I
  • Presented by Timture Choi

2
Parameter Passing
  • 4 categories
  • Pass-by-value
  • Pass-by-result
  • Pass-by-value-result
  • Pass-by-reference

3
Pass-by-value
  • Operation
  • By using additional storage for the parameters
  • The value of parameters are copied to the storage
    allocated
  • The storage memory will be destroy after the
    termination of the function
  • Characteristics
  • If a variable is passed as a parameter
  • Its value will not be changed after returning
    from the function
  • As the function only manipulates the copy of the
    variable

4
Pass-by-value
  • E.g.

void Increment(int Number) Number Number
1 cout ltlt "The parameter Number is " ltlt
Number ltlt endl int main() int i 10
Increment(i) cout ltlt "The variable i is "ltlt i
ltltendl return 0
5
Pass-by-value
  • Advantages
  • Increased the reliability of the system
  • Because the value of the parameter cannot be
    tampered
  • Disadvantages
  • If the size of the parameter is very large
  • E.g. passing a very large array
  • This method would be very inefficient
  • Only one output can be produced

6
Pass-by-reference
  • For inout mode parameters
  • Syntax
  • lttypegt ltvariablegt
  • lttypegt ltvariablegt
  • It transmits an access path
  • To the called function
  • Usually just an address
  • Instead of a copy of its value
  • If the function changes the parameter value
  • The value of corresponding argument will also be
    changed
  • Since they share the same memory location

7
Pass-by-reference
  • E.g.

void Increment(int Number) Number Number
1 cout ltlt "The parameter Number " ltlt
Number ltlt endl int main() int i 10
Increment(i) cout ltlt "The variable i is "ltlt i
ltltendl return 0
8
Pass-by-reference
  • Advantages
  • No extra storage and data copying is needed
  • Improve the efficiency
  • Value of the actual parameters can be modified
  • More flexibly
  • Provide multiple output

9
Function Summary
  • Each function can be designed, coded, and tested
    separately
  • Facilitate debugging process
  • Both pass-by-value and pass-by-reference can be
    used in the same function

10
Scope
  • Scope of identifier
  • The range of statements in which
  • The identifier can be referenced
  • E.g.
  • Scope of variable area
  • Starts just after its declaration
  • Ends at the end of the function

double area_of_circle(double radius) double
area area 3.1415 radius radius
return area
11
Scope
  • Global declaration
  • Placed at the beginning of the program file
  • Can be accessed/visible anywhere/by any function
    in the program
  • Local declaration
  • Made inside a block of code
  • Cannot be accessed outside the current block
  • E.g. only can be access within certain function

12
Scope Example
  • int y 38
  • void fun(int, int)
  • int main( )
  • int z47
  • while(zlt400)
  • int a 90
  • z a
  • z
  • y 2 z
  • fun(1, 2) return 0
  • void fun(int s, int t)
  • int r 12
  • s r t
  • int i 27
  • s i

scope of y
scope of fun
scope of z
scope of a
scope of s t
scope of r
scope of i
13
Scope
  • Note
  • Undisciplined use of global identifiers
  • May lead to confusion and debugging difficulties
  • Better to communicate values between functions
  • Through arguments/parameters
  • Instead of using global variables

14
SUMMARY
  • By the end of this lab, you should be able to
  • Implement function with both
  • Parameter pass-by-value
  • Parameter pass-by-reference
  • Identify the scope of different identifiers
  • Declared global/local variables/constants
Write a Comment
User Comments (0)
About PowerShow.com