1 / 8100%
Review - Methods
CSE-205
Recall
A method is a sequence of
instructions with a name.
Methods are used to
organize and simplify coding.
A method signature specifies
the method name, the return
type, and the number and
type of arguments required.
Methods and
Abstraction
Writing methods allows for
abstraction in problem
solving. Method abstraction
separates the use of a
method from its
implementation.
No details are necessary for
the specific tasks of getting
lottery numbers, setting the
Powerball, or displaying the
numbers. The logical flow of
the solution can be
developed and tested.
Value-returning vs
void methods
Methods are either value returning or void.
When writing a value-returning method,
ensure that all control paths return a value.
When calling a value-returning method,
ensure that the calling block of code utilizes
the return value. In most cases, value-
returning methods calculate and return
some value or set of values.
void methods do not return a value. In most
cases, void methods perform some task,
such as displaying lottery numbers.
Good Practice
Name methods according to what the method does.
Use verbs and verb phrases
getNumbers()
showNumbers()
calculateTax()
findArea()
Calling a
Method
Calling a method
executes the code
inside the method.
If a method returns a
value, the call to the
method is usually
treated like a value.
Calling a
void method
A call to a void
method must be a
statement
A call to a void method must be a statement
Putting It
Together
This example shows
a value-returning
method and a void
method in the same
statement.
Powered by TCPDF (www.tcpdf.org)
Students also viewed