Function Parameters and Scope - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Function Parameters and Scope

Description:

Mathematics, Science and Technology Department. Farquhar Center - Nova Southeastern University ... Operations on the parameter are applied to the original object ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 20
Provided by: timma8
Category:

less

Transcript and Presenter's Notes

Title: Function Parameters and Scope


1
Function Parameters and Scope
  • Computer Programming I
  • Dr. Tim Margush
  • Mathematics, Science and Technology Department
  • Farquhar Center - Nova Southeastern University

2
What is a Function?
  • A function is a program unit representing the
    abstraction of some task
  • When designing a function, you must clearly
    identify this task and
  • Identify "input" required
  • Plan the processing to be performed
  • Return a result (if required)

3
Determining Required Parameters
  • What objects must the function have access to in
    order to accomplish its task?
  • Member functions automatically have access to the
    objects declared within the class definition
  • In general, any objects needed by the function
    are specified in the parameter list and passed to
    the function as an argument

4
Function Prototype
  • The types of parameters are specified in the
    function prototype (as is the return type)
  • void showPages(void)
  • double areaOfRectangle(double, double)
  • char middleInitial(string name)
  • Naming of parameters is not required in the
    prototype, but is a form of documentation

5
Function Heading
  • Functions are defined by specifying a function
    heading followed by a program block
  • double areaOfRectangle(double x, double y)
  • return xy
  • The parameter names are required here
  • The return type and number and type of parameters
    must match the prototype

6
Scope
  • The scope of an identifier is that part of the
    program in which the identifier can be used (has
    meaning)
  • Scope (or visibility) is determined by the
    placement of the identifier's declaration
  • There are four kinds of scope
  • Block, File,Function, and Function Prototype

7
Block Scope
  • Identifiers declared within a block or in a
    parameter list of a function definition have
    block scope
  • Block scope extends from the point of the
    identifier's declaration to the end of the block
    in which it is defined
  • A parameters' scope includes the entire function
    body that follows the parameter list
  • blocks are sections of executable statements,
    beginning with , and ending with a matching

8
File Scope
  • Identifiers declared outside all blocks and
    parameter lists have File scope
  • File scope extends from the point of declaration
    to the end of the source file
  • Function names all have file scope
  • Identifiers declared with file scope are
    sometimes called global identifiers
  • Identifiers with block scope are called local
    identifiers

9
Function Prototype Scope
  • An identifier appearing in a function prototype
    parameter list has function prototype scope
  • This scope extends from the point of declaration
    to the end of the parameter list.
  • Such identifiers serve no purpose other than to
    document the intended use of a parameter

10
Function Scope
  • Statement labels are the only identifiers with
    function scope
  • A statement label is declared inside a function
    by placing the identifier followed by a colon in
    front of a statement
  • Statement labels have meaning everywhere in the
    function body in which they are declared

11
Obscured Identifiers
  • If an existing identifier is redeclared within
    its scope, then the original identifier is
    obscured for the scope of the redeclaration
  • The redeclaration must be at block scope and may
    not be in the same block of the original
    identifier

12
An Obscure Example
  • function x has file scope
  • x has function prototype scope
  • x has block scope
  • x has block scope
  • x has block scope
  • int x(double x)
  • void fun(void)
  • cout ltlt x()
  • string x 2
  • double x 2.0
  • cout ltlt x
  • int x(double x)
  • return (int) x

13
Value Parameters
  • So far, arguments to functions have been passed
    by value
  • This means the parameter receives a copy of the
    argument's value
  • A value parameter can correspond to any argument
    which is an expression representing a value of an
    appropriate type

14
Reference Parameters
  • When a parameter becomes a reference to an object
    represented by the argument, pass-by-reference is
    being used
  • In pass-by-reference, no copy of the argument is
    made
  • The parameter is an alias for the object
    represented by the argument

15
Changing an Argument's State
  • An object passed by reference can have it's state
    changed
  • Operations on the parameter are applied to the
    original object
  • Methods applied to the parameter are applied to
    the original object
  • Object's passed by value are protected
  • Changes to the parameter affect only the copy

16
Specifying Pass-By-Reference
  • The character is used after the type in the
    parameter list to indicate pass-by-reference
  • Each parameter may be specified independently as
    pass-by-value or pass-by-reference
  • int joy(float, string , double)
  • by-value, by-reference, by-value
  • void fun(int x, double y, string z)
  • by-reference, by-value, by-reference
  • Make a conscious choice!

17
An Example to Reference
radius receives a copy of r
  • void CA(int, float , float )
  • int main()
  • float r, a, c
  • cin gtgt r
  • CA(r, c, a)
  • cout ltlt c ltlt " " ltlt a
  • return 0
  • void CA(int radius,
  • float circ, float area)
  • circ 2PIradius
  • area PIradiusradius

objects a and c are modified by the function AC
circ becomes an alias for c and area for a during
execution of the function
18
Constant References
  • Pass-by-reference is more efficient that
    pass-by-value
  • The object need not be duplicated
  • Saves time and memory
  • To protect an object passed by reference from
    changes, use const
  • void fun(const int x, const string y)
  • x and y cannot be changed, so the actual
    arguments are protected, even though aliased to x
    and y

19
Principle of Least Privilege
  • Allow functions only the minimum access to
    information required to accomplish their task
  • Make appropriate use of pass-by-value and
    pass-by-reference
  • Make appropriate use of the const modifier
Write a Comment
User Comments (0)
About PowerShow.com