The Coder's Handbook

Methods

USING METHODS

What is a Method?


A method is a block of code that works toward a common purpose. They are often called functions in other languages. Think of methods as the paragraphs in your code. They serve a few purposes:

  • Helps you organize big programs

  • Lets you efficiently repeat code


Let’s consider an example of writing our own method.


public static void main(String[] args)

{

printGreeting();

printGreeting();

printGreeting();

}


public static void printGreeting()

{

System.out.println(“Hello! This is a method!”);

}


This code will output the following messages to the console:


“Hello! This is a method!”

“Hello! This is a method!”

“Hello! This is a method!”


For now, don’t worry about what public static void means. We’ll learn what some of this means later in this section, and some much later in the course.

PARAMETERS

Parameters and Arguments


A parameter is the variable received by a method. This allows us to transfer information from one part of the program to another. You list the parameter’s name and type in parentheses after the name of the method. When you’re passing data to a method, that same data is called the argument. It's sort of like the words magma and lava. They refer to the same object, but are different based on where you find the hot rock.


public static void main(String[] args)

{

int num = 5;

System.out.println(“The number is: “ + num);

printDouble(num);

System.out.println(“The number is: “ + num);

}


public static void printDouble(int number) // Method Header

{

number = number * 2;

System.out.println(“Double the number is: “ + number);

}


The number is 5

Double the number is 10

The number is 5


Let’s notice a few things about this:

  • The top line that defines a method is called the method header

  • The value stored in num is copied to the method and called number

  • This means that when you double number, it only modifies the copy, not the original

  • The name of the parameter does not have to be the same as the original variable.

Multiple Parameters


You can have a method with multiple parameters. To do this, you simply separate them with commas.


public static void main(String[] args)

{

simpleMadLib(“Fitz”, “ate”, “500”, “books”)

simpleMadLib(“Simmons”, “buys”, “32”, “pajamas”)

}


public static void simpleMadLib(String name, String verb, int num, String noun)

{

System.out.println(name + “ “ + verb + “ “ + num + “);

}


Fitz ate 500 books

Simmons buys 32 pajamas


When calling that method, make sure you list your parameters in the correct order and that they have the correct type. Don’t try and put a square peg in a round hole; you’ll get a compile error.

Volcanos

  • Lava - above ground

  • Magma - underground

Methods

  • Argument - where the method is called

  • Parameter - in the method itself

RETURN TYPES

Return Types

Just like variables, methods can have different types. All the methods we have seen so far are void which means they do not return anything. For example:


public static void main(String[] args)

{

if(canVote(22))

{

println(“You can vote!”)

}

}


But methods can return information to the method that called them. This works for any data type. The method below uses a boolean to return a true or false expression.


public static boolean canVote(int age)

{

if(age >= 18)

{

return true;

}

else

{

return false;

}

}


You can also use the return statement in a void method. Doing so will just instantly exit out of the method:


public void addVote(int age)

{

if(!canVote(age))

{

return; // this exits the method early

}

// this code below will only execute if canVote is true

votes++;

}

RESOURCES