U3: User Defined Methods Flashcards
Trial and Error
Create program, run it, alter code until desired result
Stepwise Refinement
- Completing program in trial-and-error method closer and closer to solution
- Ex: Writing first part of program, perfecting, then continuing
Divide and Conquer
Breaks task into smaller parts
Modular Programming
- Building large programs through combining separate chunks of code
- Possible by creating main program, then creating separate subroutines for program
Method Header
- public static void [method] (data)
- AKA: Subroutine definition
Method Body
Code of the method
Modifiers
Ex: public and static
public/private
- Access specifier stating where subroutine can be called from
- Private: Just within selected part of program
- Public: Other parts
Naming Conventions for Subroutine
- Begin with lowercase
- Camel notation
- Descriptive name
- No reserved words
Static
Ensures subroutine can exist on own and not as part of object
Local Variables Inside Subroutine
- Different, simpler name from ones in main program
- Only exist within subroutine
- Wiped from memory when subroutine done executing
Program Control for Subroutine
- Program control transferred to subroutine when latter is called
- Transferred to main program when subroutine done executing
Procedure
Performs task but doesn’t return data to main program; returns void
Data Transfer from Main Program to Subroutine
Write arguments in brackets in same order as parameters
Function
- Performs task and returns data to main program
- Return type specified and return statement concludes subroutine
Return Types
Int, Double, String, Void, etc
void
Used when no data returned to main program
Return Statement
return (variable);
Outputting Data from Method Call
- Output to screen on the same line as the method call OR
- Use variable in main program to store data
Methods
- Declared within main class
- Include both procedures and functions
- Can’t return +1 piece of data
Declaring Parameter
Done while declaring subroutine
Declaring Argument
Done while calling subroutine
Declaring Variables on Same Line
Possible for same data type in the main program, not in subroutines
Subroutine Header
Comments explaining subroutine function, inputs and outputs, parameters passed, data returned (if any).