Parameters, Functions, and Methods From wikipedia - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

Parameters, Functions, and Methods From wikipedia

Description:

An accessor is a method that accesses data from an ... Calling an accessor is always ... of what it means to write a mutator, accessor, and 'other' method ... – PowerPoint PPT presentation

Number of Views:55
Avg rating:3.0/5.0
Slides: 10
Provided by: stevenb7
Category:

less

Transcript and Presenter's Notes

Title: Parameters, Functions, and Methods From wikipedia


1
Parameters, Functions, and Methods From
wikipedia
2
Expressions vs. Statements
  • Expression
  • Generates a new value
  • In the shell, shows a result
  • If x something makes sense, then something is
    an expression
  • More precisely, if x does not become None, then
    something is an expression
  • def myFunction() return ____means that
    calling myFunction() is an expression
  • Statement
  • Executed for side effect
  • In the shell, shows no result
  • print is kind of a special case, but not really
  • If x something doesnt make sense, then
    something is a statement
  • More precisely, if x becomes None, then something
    is a statement
  • def myFunction() ltno return anywheregt
    means that calling myFunction() is a statement
  • If you treat a statement as if it were an
    expression, then the statement gets executed (the
    side-effect occurs), and the statement
    evaluates to None.
  • That is, if a statement is put in a context that
    requires an expression, the value None will be
    used.

3
Parameter Passing
  • Formal parameters
  • The names of the parameters in the function (or
    method) definition
  • Actual parameters
  • The values passed in to a function (or method)
    invocation
  • When you understand the following, you can figure
    out any parameter passing issue
  • Parameter passing works asformalParam
    actualParam
  • works as copy the box
  • Copying for primitive types (int, float, long, )
  • Aliasing for reference types (string, list,
    Point, )
  • Watch exact description of functions / methods to
    see if its a mutator or not

4
Consequences of the Rules on Parameter Passing
  • Primitive types (int, long, float, some others)
    cant be changed
  • All other mutable types can be changed (class
    instances, lists) using mutators
  • So things like move(), and myListi ___
  • These mutate will change the parameter
  • If you construct a new object, then its a new
    object nothing you do to the new object will
    affect the original

5
Methods
  • A method is like a function, except that it is
    associated with an object.
  • These are methods
  • s.capitalize() for a String s
  • p.getX() for a Point p
  • p.setFill(c) for a Point p
  • These are not methods (they are merely
    functions)
  • string.capitalize(s)
  • abs(n)
  • math.sqrt(n)

6
Accessors vs. Mutators
  • An accessor is a method that accesses data from
    an object
  • Ex p.getX()
  • Often start with get (style)
  • Calling an accessor is always an expression
  • A mutator is a method that changes data internal
    to an object
  • Ex p.setFill()
  • Often start with set (style)
  • But not necessarily p.move(dx, dy)
  • Calling a mutator is (typically) a statement
  • Strictly speaking, accessors and mutators only
    work on objects. That is, they are methods, not
    merely functions.
  • objName.methodName()

7
Is It A Mutator?
  • What do you have to do to make the change happen?
  • Nothing extra?
  • p.move(5, 5)
  • move() IS a mutator
  • Use ?
  • s s.upper() (p. 96)
  • upper IS NOT a mutator
  • Often, but not always
  • Mutator does not return anything. Its a
    statement.
  • Non-mutator returns something. Its an
    expression.

8
Other Methods
  • Some methods are neither accessors nor mutators,
    or it is less clear.
  • The distinction gets less important here
  • p.draw(win) ?
  • Mutates win, in a sense, but the Point object
    itself doesnt change (?), so it isnt a mutator
    for Point.
  • Unless Point maintains a list of windows its
    drawn on, in which case this would mutate Point.
  • We only speak of mutators and accessors with
    respect to the object on the left of the dot.

9
Writing Functions and Methods
  • So far, weve used both methods and functions
  • and were just now learning the details of
    writing functions.
  • Well learn how to write methods later on.
  • Once we do that, well see more of what it means
    to write a mutator, accessor, and other method
Write a Comment
User Comments (0)
About PowerShow.com