Name the parts of a method
A program module that contains a series of statements that carry out a task
Invoke or call a method from another program or method
Any program can contain an unlimited number of methods
Each method can be called an unlimited number of times
Client- calling a program or method is called a method’s client
A method must include:
Method header (also called declaration or definition), includes identifying information
Method body contains the implementation (statements that carry out the task)
Method return statement- returns control to the calling method
Local variables are declared in a method
Keeping variables local helps them be more portable and less prone to error
Global variables are known to all modules
In scope, only recognised in a method
Design methods with no parameters
When methods must share data
Pass the data into and return the data out of methods,
When you call a method from a program, you must know 4 things:
What the method does, why are you calling the method
Name of the called method
Type of information to send to the method if any
Type of return data to expect from any method if any
Design methods that require parameters
Argument to the method- pass a data item into a method from a calling program
Parameter to the method- method receives the data item
When a method receives a parameter, you must provide a parameter list that includes:
The type of parameter
The local name for the parameter
Signature- method’s name and parameter list
Passed by value- copy of value is sent to method and stored in a new memory location
accessible to module
The value remains unchanged in the main program even if it has been changed in the
module
Each time a method executes, parameter variables listed in the method header are redeclared
, myMethod (num myValue)
Methods can require more than one parameter
computerTax (num amount, num rate)
List the arguments within the method call, separated by commas
List a data type and local identifier for each parameter within the methods header’s
parentheses
The data type must be repeated with each parameter
Methods called, parameters have to be in the correct order, otherwise there will be logical
errors
computerTax (num amount, num rate) cannot be called as computerTax (rate, amount)
Arguments sent are called actual parameters
Variables that accept the parameters in the method are called formal parameters
Design methods that return a value
A variable declared within a method ceases to exist when the method ends (goes out of scope)
To retain a value that exists when a method ends, return the value from the method back to
the calling method
When a method returns a value, the method return type must match the data type of the value
that is returned
Return type for a method
Indicates the data type of the value that the method will send back
Can be any type
Also called a method’s type
Listed in front of the method name when the method is defined
Method can also return nothing
Return type void
Void method
Example: num getHoursWorked (), returns numeric value
Usually, you want to use the returned value in the calling method
Not required Output “Hours worked is”,
Store in variable or use directly without storing getHoursWorked ()
Technically you are allowed to include multiple return statements in a method
It’s not recommended
Violates structured logic
To use a method, you should know:
What the method does in general
The method’s name
The method’s required parameters, if any
The method’s return type, so you can use returned values appropriately
Overhead refers to the extra resources and time required by an operation, such as calling a
method