Skip to main content
  • Statement = a line of code that performs a basic operation
  • Method = a named sequence of statements
  • Usage:
    • to do non-simple operations repeatedly
    • to break complex problems into smaller, simpler parts

Types of Methods

Void Method

  • Method Names Method Names
  • Parameters and Arguments Parameters and Arguments
  • Statements Statements
  • Overloading Overloading

Exercise Void Method

Write one or several methods to do the following
  1. Prints a String n times
  2. Display the message “Good Morning” in various languages
  3. Display an alphanumeric character as an ASCII art

Value Method

  • Return type and value Return type and value
  • 2

Exercise Value Method

Write one or several methods to do the following
  1. Calculates the distance between two coordinates
  2. Determines whether a number is prime or not
  3. Determines the number of days in a year

Method Composition

Recursive Methods

  • A method that invokes itself
  • example:
  • Write the base case:
  • Write the reduction step. Ensure that the base case can be reached:

Recursion vs Iteration

  • Recursion is not always needed
  • Everything that can be done with recursion can be done using iterations (loops)
  • Recursion
    • consumes more memory and slower than iterations
    • elegant, shorter code

Documentation

True