Title: CS 303E Lecture 11: How Methods Communicate
1CS 303E Lecture 11How Methods Communicate
"I wish people (or programs) who have trouble
communicating would just shut up." --Tom Lehrer
2Parameters and Return Values
- Parameters are used to send information to
methods. - Return values are used to receive information
from methods.
parameters
return value
Method
3Passing and Returning Values
public void buttonClicked (Button buttonObj)
int months monthsField.getNumber() double
rate rateField.getNumber() / 12 double
initialAmount initialAmountField.getNumber()
double totalAmount initialAmount while
(months gt 0) totalAmount totalAmount
rate totalAmount months months - 1
totalAmountField.setPrecision (2)
totalAmountField.setNumber (totalAmount)
returnField.setPrecision (2)
returnField.setNumber (totalAmount -
initialAmount)
getNumber returns a value setPrecision is
passed a value setNumber is passed a value
buttonClicked is passed a value
4Method Signatures (Headers)
public void buttonClicked (Button buttonObj)
Access Return Method Parameter
Parameter modifier type name type
name public void setNumber (double
number) public double getNumber ()
private double computeArea () A return type of
void means that no value is returned.
5Design Principles
- Determine how a method will be used
- Information sent via parameters.
- Information returned.
- Write the methods signature.
- Implement the methods body.
6Example Compute Total Amount
public void buttonClicked (Button buttonObj)
int months monthsField.getNumber() double
rate rateField.getNumber() / 12 double
initialAmount initialAmountField.getNumber()
double totalAmount initialAmount while
(months gt 0) totalAmount totalAmount
rate totalAmount months months - 1
totalAmountField.setPrecision (2)
totalAmountField.setNumber (totalAmount)
returnField.setPrecision (2)
returnField.setNumber (totalAmount -
initialAmount) Information sent
initialAmount, months, rate Information
returned totalAmount
subtask
7Step 1 Determine the Use
public void buttonClicked(Button buttonObj)
int months monthsField.getNumber() double
rate rateField.getNumber() / 12 double
initialAmount initialAmountField.getNumber()
double totalAmount computeTotal
(initialAmount,
months, rate) totalAmountField.setPrecisio
n (2) totalAmountField.setNumber
(totalAmount) returnField.setPrecision (2)
returnField.setNumber(totalAmount -
initialAmount) Information sent
initialAmount, months, rate Information
returned totalAmount
8Step 2 Write the Signature
- // Task Computes the total investment amount
after a - // period of months at a given rate.
- // Inputs The initial amount invested, the
number of - // months, and the annual rate.
- // Returns The total amount of investment.
- public double computeTotal (double initialAmount,
- int months, double
rate) - Comments describe
- what the method does and
- how it communicates with its caller.
9Step 3 Implement the Body
// Task Computes the total investment amount
after a // period of months at a given
rate // Inputs The initial amount invested, the
number of // months, and the annual
rate // Returns The total amount of
investment public double computeTotal (double
initialAmount, int
months, double rate) double totalAmount
initialAmount while (months gt 0)
totalAmount totalAmount rate totalAmount
months months - 1 return
totalAmount
10Structure Chart for Investment
Method buttonClicked
Output or return value
Inputs
initialAmount months rate
totalAmount
Method computeTotal
11Parameters
Formal parameters are declared in the method
definition public double computeTotal (double
initialAmount, int
months, double rate) . . .
Actual
parameters provide values for the formal
parameters in the method call computeTotal
(2500.25, 36, 0.5) // Actual
parameters computeTotal (150, 24, 0.10)
// int to double OK computeTotal (150, 24.5,
0.10) // double to int ERROR Actual
parameters must match formal parameters in
number, order, and type. But less inclusive
types (like int) are automatically converted to
more inclusive types (like double).
12How a Parameter Is Passed
- The computer evaluates the actual parameter. The
parameter may be a literal, a constant, or an
expression. - The value is copied into temporary storage for
the formal parameter. - The method works on this storage for the formal
parameter. - The formal parameter is used just like a local
variable in the method.
13Storage for Investment
Storage for buttonClicked
Storage for computeTotal
initialAmount
250.45
initialAmount
250.45
months
months
24
24
rate
rate
0.05
0.05
totalAmount
totalAmount
250.45
When computeTotal is done all local variables and
parameters cease to exist.
14Parameters, Locals, and Globals
- Parameters behave just like local variables.
- Parameters are initialized by the methods
caller. - Parameters can have the same names as globals,
but not the same names as locals. If a parameter
has the same name as a global / instance
variable, the parameter shadows the global /
instance variable.
15Input-Only Parameters
- Parameters of type int, double, and other
primitive types cannot be used to return values
to the methods caller. The local copy can be
changed, but the original argument is not changed - Thus, a method can return at most one value of a
primitive type. (Parameters of primitive types
do not cause anything to be permanently altered.)
16Access to Globals
- Methods that modify global variables should not
receive these variables as parameters (when they
are primitive types). - Methods that merely use a global can receive the
global as a parameter or access the global
directly. - Remember, from a design standpoint we want to
minimize the number of global / instance
variables to those that must survive any method
calls
17 Use globals directly private void
displayAccount() messageBox ("Name "
name "Id " idNumber
"Balance " balance) displayAcco
unt() Or use parameters and have the caller pass
the globals private void displayAccount (String
n, String id, double
bal) messageBox ("Name " n
"Id " id "Balance "
bal) displayAccount(name, idNumber,
balance)
18Returning a Value
- Use the return statement. E.g.
- return totalAmount
- (return is a reserved word.)
- Execution of the method ends with the execution
of the return statement. - When methods end execution goes to the next
statement after the method was called. - There may be several return statements if
convenient.
19The returned value
- The returned value must be of the same type as
declared in the method header. - public double computeTotal (double initialAmount,
- int months, double
rate) - double totalAmount initialAmount
- while (months gt 0)
- totalAmount totalAmount rate
totalAmount - months months - 1
-
- return totalAmount
20CS 303E Lecture 12More on Methods
"We are what we think. All that we are arises
with our thoughts. With our thoughts, we make the
world." -Buddha
21More on Methods
- So far we have looked at void methods.
- Methods can also have a return type
- The type of information returned by the method
- public int power(int base, int exponent)
-
- int is the return type. When called this method
will give back an int - The data type may be any data type, primitive
data type or object type.
22The return Statement
- public int power(int base, int exponent)
-
- int result 1
- if(exponent lt 0)
-
- return 0
-
- else
-
- int count 1
- while(count lt exponent)
-
- result result base
- count count 1
-
-
- return result
-
23The return Statement
- When a return statement is encountered the value
after the word return is sent back to the place
where the method was called from. - The value returned may be a variable, a literal
constant, or an expression. - The return statement may appear more than once in
the method. - When a return statement is executed the method is
complete, even if there is code left.
24Calling Methods that Return a Value
- If a method returns a value that method call may
be used anywhere in the program where the return
type could be used. For example - int x, y, z
- x power(2, 3)
- y 2
- z power(x, y)
- answerField.setNumber( power(z, 3) )
- // or even
- z power( power(2, 3), power(3, 2) )
- //!!!!!
25Changing power
- How do we change power so that it works with
bases that are doubles as well? - Does this make the method more useful or less
useful. Why?