Vous êtes sur la page 1sur 4

Methods

Code Reuse:
Methods in Java
CSE 110: Introduction to Computer Science
Stony Brook University

A method is a block of code that can be


reused within a program

Its like a mini-program inside the main


program

Use methods to break up a task into


smaller parts

This is especially useful if you need to

repeat an operation with different data

Methods are Functions

A function maps (translates) some input value


to a specific output value

We always map input to output the same way

e.g., f(x) = x
Given the same input, it will always return
2

the same output

Calling Methods
To call a method, write its name, followed by
its arguments (if any) in parentheses:

System.out.print(Hello world!);
Math.pow(2, 3);

The order of arguments is important!


When a method is called, the code that called
it is put on hold while the method executes

Defining a Method

Method Input/Output

The first line is the method header


Describes the methods characteristics
<return type> <name> ( <parameters> )
Ex. int foo (double x)
foo() takes a double value as its input, and

A method can take input values (called

void indicates that no value is returned

Input and output are optional for methods

Aside: Arguments vs.


Parameters

The Method Body

returns an integer value

When you call a method, the values you


pass in as input are called arguments

Inside the method, they are called


parameters

These are like local variables inside the


method, but they get their values from
outside the method

arguments or parameters)

The method uses these values in its work

A method can also return a value


It can send back the result of its
calculation to the main program

Enclosed in curly braces immediately


following the method header

Contains the code that will be run each


time the method is called (invoked)

Can declare its own variables, or use the


methods parameters (from the header)

These values are redefined each time the


method is called

Return Values
If a method has a return type other than

A void Method

void, it MUST contain a return statement

This sends some value back to the code

void printLineOfStars()

that originally called the method

The invoking method can use or ignore it

System.out.println(**********);

Format: return <value or expression>;


A method immediately ends when it sees a

} // NOTE: no return statement needed

return statement!

Return Type Example

Another Method Example

int timesThree (int value) // return type: int


{
// The type of the return value MUST match
// the return type in the method header
return value * 3;
}

int multiply (int first, int second) // header


{
return (first * second);

// body

void Methods with


Input
Methods can take input even if they don't
explicitly return a value

instead, they have side effects

A side effect affects a program's output or


state, but is not directly detectable by the
program

e.g., printing something to the screen

The main() Method


Every Java program has a special method
called main()

Execution begins with this method

main() has a special (required) header:


public static void main (String [ ] args)

You can replace args with any variable name

Side Effect Example


void welcome (String name)
{
// Note: we have a side effect,
// but no return value
System.out.print("Welcome, ");
System.out.println(name + "!");
}

Vous aimerez peut-être aussi