Methods and Parameters - PowerPoint PPT Presentation

About This Presentation
Title:

Methods and Parameters

Description:

Example : Method header for drawOval from p.741 (Blue book) ... activation of a method actual values representing the parameters are specified. ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 11
Provided by: kumarm8
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Methods and Parameters


1
Methods and Parameters
  • B.Ramamurthy
  • SUNY at Buffalo

2
Introduction
  • Methods represent the functionality of objects.
  • You can customize the functions carried out by a
    method using a powerful mechanism called
    parameter passing.
  • This concept was briefly introduced in the last
    set of lectures on Classes and Objects. We will
    learn more details about parameter passing in
    this discussion.

3
Topics for Discussion
  • 1. Why parameters?
  • 2. Types of parameters formal and actual
  • 3.How to specify the parameters?
  • 4. Semantics of parameter passage
  • 5. Return statement and return value
  • 6.Example
  • 7. Summary

4
Why Parameters?
  • Parameters are needed to specialize a method
  • For example Consider drawOval
  • Without any parameters it will drawing any one
    fixed size oval.
  • By specifying parameters drawOval(40,30,20,100)
    you are able to set the size as well the position
    of your oval.
  • This is the power that parameters offer a method
    invocation.

5
Formal Parameters
  • Parameters defined in the method header are
    called formal parameter.
  • Example Method header for drawOval from p.741
    (Blue book)
  • public abstract void drawOval ( int xsrc, int
    ysrc, int width, int height)
  • Meaning Formal parameter list gives the type and
    name of characteristics that can be changed in
    the method invocation.
  • Formal parameter are dummy and do not get any
    value until a method is activated/invoked.

6
Actual Parameters
  • During the call /invocation/ activation of a
    method actual values representing the parameters
    are specified.
  • Example
  • drawOval ( 20, 30, 50. 50)
  • X 4 Y 60
  • Z Math.pow(X , 2) A X Y
  • drawOval(X, Y, Z , A)
  • drawOval( X, Y, Math.pow(X,2), XY) //
    Complex expressions as parameters

7
Semantics of Parameter Passage
Definition
Call / Invocation
int Sum ( int X, int Y)
Total Sum (67, 60)
return (X Y)
8
Semantics (contd.)
  • Number of formal parameters should be same as
    actual parameters.
  • When a method is invoked the actual parameters
    are positionally matched to the formal
    parameters.
  • Type of actual parameters should be same as
    corresponding formal parameters.
  • Value of the actual parameters are copied into
    the formal parameters.
  • Control is transferred to method.
  • Execution resumes in the method.
  • When a return statement is encountered the value
    specified is returned and control is transferred
    back to the caller.

9
Void and non-void Methods
  • Void methods do not return a value, and so can be
    called as a stand-alone
  • Example drawOval(5, 4 ,6 ,6)
  • non-void methods return a value, so are usually
    used in an expression or assignment statement, or
    in general, in place of a variable.
  • Example
  • X Math.abs(X)
  • Card RandGen.nextInt()

10
Example
Write a Comment
User Comments (0)
About PowerShow.com