toblick / cs101 / methods
+ - 0:00:00
Notes for current slide
Notes for next slide

Methods

"Take a method and try it. If it fails, admit it frankly, and try another. But by all means, try something."

-Franklin D. Roosevelt

1 / 17

Overview

3 / 17

Overview

Concept

Methods are modular reusable blocks of code.

3 / 17

Overview

Concept

Methods are modular reusable blocks of code.

  • The control flow of a program can easily switch to code within a method from anywhere else in the code.
3 / 17

Overview

Concept

Methods are modular reusable blocks of code.

  • The control flow of a program can easily switch to code within a method from anywhere else in the code.

  • Switching to code within a method is termed 'calling' or 'invoking' that method.

3 / 17

Overview

Concept

Methods are modular reusable blocks of code.

  • The control flow of a program can easily switch to code within a method from anywhere else in the code.

  • Switching to code within a method is termed 'calling' or 'invoking' that method.

  • Calling a method is a form of unconditional branching - disrupting the 'usual' flow of a program.

3 / 17

Overview

Concept

Methods are modular reusable blocks of code.

  • The control flow of a program can easily switch to code within a method from anywhere else in the code.

  • Switching to code within a method is termed 'calling' or 'invoking' that method.

  • Calling a method is a form of unconditional branching - disrupting the 'usual' flow of a program.

  • Once the control flow reaches the end of an invoked method, the control flow returns to the line of code from which it originally branched.

3 / 17

Why methods?

4 / 17

Why methods?

  • methods allow us to split a large program into manageable (read: small) sub-problems - "divide and conquer".
4 / 17

Why methods?

  • methods allow us to split a large program into manageable (read: small) sub-problems - "divide and conquer".

  • modular code is easier to read.

4 / 17

Why methods?

  • methods allow us to split a large program into manageable (read: small) sub-problems - "divide and conquer".

  • modular code is easier to read.

  • modular code is re-usable code.

4 / 17

Why methods?

  • methods allow us to split a large program into manageable (read: small) sub-problems - "divide and conquer".

  • modular code is easier to read.

  • modular code is re-usable code.

  • modular code is easier to write and debug.

4 / 17

Method abstraction

5 / 17

Method abstraction

  • A user (e.g. you) can use a method without knowing its implementation. The only thing the user needs to know is the methods signature (how to call it) and return type.
5 / 17

Method abstraction

  • A user (e.g. you) can use a method without knowing its implementation. The only thing the user needs to know is the methods signature (how to call it) and return type.

  • If the implementation changes "behind the scenes", the user does not need to modify their code that builds on the method (as long as the signature did not change).

5 / 17

Simple Methods

6 / 17

Simple Methods

Super simple

At their simplest, methods can have no parameters and no return value.

For example, a method named 'doSomething1':

public static void doSomething1() {
System.out.println("Running doSomething1");
// imagine some useful stuff happens in the middle here
System.out.println("Exiting doSomething1");
}
6 / 17

Defining methods

Methods are comprised of a header and a body. Syntax:

modifiers returnType methodName(parameters) {
[...] // stuff happens here
return result;
}

The returnType can be any data type or void (i.e. the method does not return a value).

7 / 17

Simple Methods

Methods calling methods

Of course, methods can call other methods, as our doSomething1 already displayed - it called the System.out.println method several times.

Note the order in which the print statements are executed.

public static void doSomething1() {
System.out.println("Begin doSomething1");
// imagine some useful stuff happens in the middle here
System.out.println("End doSomething1");
}
public static void doSomething2() {
System.out.println("Begin doSommething2");
doSomething1(); // call the method
System.out.println("End doSomething2");
}
8 / 17

Simple Methods

Call stack

The order is determined by the control flow of the program.

9 / 17

Simple Methods

Call stack

The order is determined by the control flow of the program.

  • Each method invocation creates a new 'stack frame' - an area of memory dedicated to the newly invoked method.
9 / 17

Simple Methods

Call stack

The order is determined by the control flow of the program.

  • Each method invocation creates a new 'stack frame' - an area of memory dedicated to the newly invoked method.

  • The Java interpreter is only ever looking at the method invocation at the top of the stack - the most recently invoked method.

9 / 17

Simple Methods

Call stack

The order is determined by the control flow of the program.

  • Each method invocation creates a new 'stack frame' - an area of memory dedicated to the newly invoked method.

  • The Java interpreter is only ever looking at the method invocation at the top of the stack - the most recently invoked method.

  • Each stack frame has its own variable namespace, so you can have two variables named x in two different method invocations, and they will be different variables in two different areas of memory with potentially different values.

9 / 17

Simple Methods

Call stack

The order is determined by the control flow of the program.

  • Each method invocation creates a new 'stack frame' - an area of memory dedicated to the newly invoked method.

  • The Java interpreter is only ever looking at the method invocation at the top of the stack - the most recently invoked method.

  • Each stack frame has its own variable namespace, so you can have two variables named x in two different method invocations, and they will be different variables in two different areas of memory with potentially different values.

  • Once a method that has been invoked completes, its stack frame is popped (deleted) from the call stack and its memory is wiped clean.

9 / 17

Parameters

10 / 17

Parameters

Concept

Methods can accept 'arguments' - values sent into the method.

10 / 17

Parameters

Concept

Methods can accept 'arguments' - values sent into the method.

  • These arguments are stored in 'parameters' - local variables within the namespace of the method invocation's stack frame.
10 / 17

Parameters

Concept

Methods can accept 'arguments' - values sent into the method.

  • These arguments are stored in 'parameters' - local variables within the namespace of the method invocation's stack frame.

  • Once the method invocation completes, any local variables, including parameters, are wiped out of memory.

10 / 17

Parameters

Concept

Methods can accept 'arguments' - values sent into the method.

  • These arguments are stored in 'parameters' - local variables within the namespace of the method invocation's stack frame.

  • Once the method invocation completes, any local variables, including parameters, are wiped out of memory.

public static void doSomething1(int x) {
x++; // increment x
System.out.println("Begin doSomething1, x=" + x);
System.out.println("End doSomething1, x=" + x);
}
10 / 17

Arguments

Arguments are passed by value. Their value gets copied to variables that are specific to that method.

11 / 17

Arguments

Arguments are passed by value. Their value gets copied to variables that are specific to that method.

public static void addOne(int x) {
x++; // increment x
}
public static void main(String[] args) {
int i = 1;
addOne(i);
System.out.println("i is " + i); // i is 1
}
11 / 17

Arguments

12 / 17

Arguments

This naming can be confusing, since the value of an array variable is (essentially) its address in memory:

public static void addOne(int[] array) {
for (int i = 0; i < array.length; i++) {
array[i]++;
}
}
public static void printarray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
System.out.print("\n");
}
public static void main(String[] args) {
int[] array = {1,2,3};
printarray(array); // prints 123
addOne(array);
printarray(array); // prints 234
}

We will discuss this further in the coming weeks.

12 / 17

Return values

13 / 17

Return values

Concept

Methods can return a single value in Java.

13 / 17

Return values

Concept

Methods can return a single value in Java.

  • The method signature line must state the data type of the returned value
13 / 17

Return values

Concept

Methods can return a single value in Java.

  • The method signature line must state the data type of the returned value

  • This value is returned to the part of the code from which the method was originally invoked.

13 / 17

Return values

Concept

Methods can return a single value in Java.

  • The method signature line must state the data type of the returned value

  • This value is returned to the part of the code from which the method was originally invoked.

  • Think of it as if the method invocation is replaced by that method's return value.

13 / 17

Return values

Concept

Methods can return a single value in Java.

  • The method signature line must state the data type of the returned value

  • This value is returned to the part of the code from which the method was originally invoked.

  • Think of it as if the method invocation is replaced by that method's return value.

public static int doSomething1(int x) {
x++; // increment x
System.out.println("Begin doSomething1, x=" + x);
System.out.println("End doSomething1, x=" + x);
return x;
}
13 / 17

Overloading

14 / 17

Overloading

Concept

Java allows multiple methods with the same name but different parameter sets. These are called overloaded methods.

14 / 17

Overloading

Concept

Java allows multiple methods with the same name but different parameter sets. These are called overloaded methods.

  • Which version of the method is invoked depends upon the arguments in the method call.
14 / 17

Overloading

Concept

Java allows multiple methods with the same name but different parameter sets. These are called overloaded methods.

  • Which version of the method is invoked depends upon the arguments in the method call.
public static void foo() {
...
}
public static void foo(String bar) {
...
}
public static void foo(String bar, boolean baz) {
...
}
14 / 17

Documentation

15 / 17

Documentation

Javadoc allows us to write documentation so users understand what our methods do. Take the following example:

/**
* Computing the square root with Heron's method.
* @param x a strictly positive number.
* @return the square root of x.
*/
public double myRoot(double x) {
if (x == 0.0) {
return x;
} else if (x < 0.0) {
System.out.println("Error: Negative number entered.");
return 0.0;
// this is not ideal but we will discuss exception handling later
}
int max_it = 10;
double s = 0.5 * x;
for (int i = 1; i <= max_it; i++) {
s = 0.5 * (s + x/s);
}
return s;
}
15 / 17

16 / 17

Conclusions

17 / 17

Conclusions

You now have a basic understanding of methods in Java.

17 / 17

Conclusions

You now have a basic understanding of methods in Java.

17 / 17
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow