Back to GT CS-1331

Methods:

  • Makes reusing code more efficient

    • Change values, not hardcoded

    • Reuse code without having to rewrite it

  • Return’s a value (excluding void)

    • return statement ends the method

Method Header:

  • Creates methods

  • Contains return type, method name, parameters

//format
returnType methodName(parameters){
}
//example
String search(String target, String[] list){
    return String
}
void search(String target, String[] list){
    //no return value
} 

Calling/Running Methods:

//Format
Var = methodName(inputtedParameters);
System.out.println(methodName(inputtedParameters));
//Example
String[] test = new String[5];
String a = search("hi", test);
System.out.println(search("hi", test));
 

External Method Calls:

  • Can call methods from other files

    • Both files must be in the same directory

Method Overloading:

  • Methods that contain the same name but different in parameters

    • Different types, order, and/or number of formal parameters

    • Return values don’t count as Method Overloading

  • Calling is the same as normal methods

public static boolean searchArray(String target, String[] array) 
public static boolean searchArray(int target, int[] array)