Introduction to Algorithmic Processes CMPSC 201C Fall 2000 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Introduction to Algorithmic Processes CMPSC 201C Fall 2000

Description:

Sections 3, 4, 5, 6, 9,and 10 (John and Pyush) will be in 102 Forum and ... Recursive definition of a cow. A cow is a four-legged animal whose mother was a cow. ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 23
Provided by: CenterforA152
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Algorithmic Processes CMPSC 201C Fall 2000


1
Introduction to Algorithmic ProcessesCMPSC
201CFall 2000
2
Administrative Issues
  • Exam 2 - Monday, November 6 at 815 to 930 in
    102 and 105 Forum
  • Sections 3, 4, 5, 6, 9,and 10 (John and Pyush)
    will be in 102 Forum and sections 1, 2, 7, 8,
    11,and 12 (Anand and Nidhi) will be in 105 Forum.
  • Conflict form must be returned to me by noon on
    October 25!
  • Two practice exams will be on web and I-drive by
    October 25.

3
Types of parameters
  • Formal parameters(arguments) - parameters listed
    in the function heading
  • Actual parameters(arguments) - parameters listed
    in the function call

4
Example Cont.
  • //Function to calculate miles per gallon for a
    tank of gas.
  • //
  • int mpg(double oldOdom, double newOdom,
  • double gallons) // function heading
  • int ans (int) ((newOdom - oldOdom)/gallons))
  • return ans

5
Scope
  • Scope - portion of code in which the identifier
    name is accessible.
  • Local - limited to a specific block of statements
    in which the identifier was declared (or defined
    for a function name)
  • Global - may be accessed from any portion of code
    once the identifier has been declared (usually at
    beginning of code).
  • Usually variables are local and constants are
    global.
  • If you do use global variables, be VERY careful
    as they can be affected in ways you did not
    expect.

6
Block
  • Statements that are enclosed braces .
  • May be body of function, loop, if-then, etc.

7
Scope Rules
  • A name that is declared within a block is local
    to that block and is accessible only within that
    block.
  • All other names are inherited from the
    immediate surrounding region.

8
Lifetime
  • Lifetime of a variable or constant is the time
    when memory has been allocated to it.
  • If the constant or variable is global, memory is
    always allocated to that constant or variable and
    the lifetime would be static.
  • If the constant or variable is local, then memory
    is only allocated when the block in which it was
    declared is executing and the memory is
    deallocated when the block is finished executing.
    In this case the lifetime would be automatic.

9
Program Example
10
Questions????
11
Multiple Results
  • Value- returning function returns one value to
    the function that called it.
  • However, sometimes more that one result is
    calculated and need to be passed back to the
    originating function.
  • Then use reference parameters (arguments) in the
    formal parameter list.

12
Example (pg. 149)
  • // Add two vectors that are at right angles..
  • // Pi is a global CONSTANT that was declared
    before
  • // main
  • void addVect (double a, double b //vectors at rt
    lts
  • double rMagnitude // output
  • int rDirection ) // output - direction
  • double rDirRadians //local variable
  • rMagnitude sgrt(a a b b)
  • rDirRadians atan (b / a)
  • rDirection (int) (180 / Pi rDirRadians
    0.5)

13
Function Heading
  • Pass by value
  • int mpg(double oldOdom, double newOdom,
  • double gallons)
  • Pass by reference
  • void addVect (double a, double b //vectors at rt
    lts
  • double rMagnitude // output
  • int rDirection ) // output - direction

14
Function Prototype
  • Pass by value
  • int mpg (double, double, double)
  • Pass by reference
  • void addVect (double, double, double, int)

15
Function call
  • Pass by value
  • lastTankMPG mpg(10502.5, 10754.6, 10.0)
  • Pass by reference
  • addVect (aVect, bVect, rMag, rDir)

16
Pass by reference
  • When using pass by reference, both identifiers
    (the one in the function call and the one in the
    function heading) refer to the same memory
    location.
  • Therefore, when formal parameter (the one in the
    function) changes what is stored in the memory
    location, the same change occurs for the actual
    parameter

17
Formal and Actual Parameters
  • Formal Parameter Actual Parameter
  • Pass by value value, variable, or an
  • expression
  • Pass by reference variable

18
Value vs Nonvalue
  • Use value-returning functions (int, float,
    double, etc.) when one value needs to be passed
    back to the calling function.
  • Use nonvalue-returning functions (void) for
    printouts, pass multiple results, modify actual
    parameters.
  • See table 5.2 on page 153

19
Questions????
20
Recursive Function
  • A function that calls itself or is part of a
    cycle of calls.

f1
f1
f3
f2
21
Non-programming Example
  • Recursive definition of a cow.
  • A cow is a four-legged animal whose mother was a
    cow.

22
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com