Chapter 6 Methods Flashcards
What is a method and what are they used for:
A method is a sequence of program statements that have a specific task that they perform.
Methods can be used to define reusable code, organize and simplify coding, and make code easier to maintain.
Creating a method involves writing the statements and providing a method declaration with:
- Method name (following the same rule as identifyers)
- List of inputs (called parameters) and their data types
- The output (return value) type
Defining a method syntax:
modifier returnValueType methodName(list of parameters){
//method body
return returnValue;
}
Calling (or invoking) a method involves
- Providing the name of the method
- Providing the arguments (inputs) if any
- Providing variable to store the outputs (if any)
Syntax:
variable = methodName(list of arguments);
A method is a:
A method is a collection of statements that are grouped together to perform an operation
Method signature
Refers to the combination of the method name and the parameter list
Formal parameters
The variables defined in the method header are known as the formal parameters
Actual parameters
When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument.
x,y in int z = max (x, y);
Return Type Value 14