• No results found

Methods Functions

N/A
N/A
Protected

Academic year: 2020

Share "Methods Functions"

Copied!
54
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Methods / Functions

A method/function is a set of statements

grouped together to perform an operation

Useful to break code into methods/functions to

keep code simpler

(3)

Method Components

Name

Modifiers

Return type

(4)

Method Example

public class HelloWorld

{

public static void main(String args[])

{

//code

}

}

(5)

Method Example

public class HelloWorld

{

public static void main(String args[])

{

//code

}

}

(6)

Method Example

public class HelloWorld

{

public static void main(String args[])

{

//code

}

}

(7)

Method Example

public class HelloWorld

{

public static void main(String args[])

{

//code

}

}

(8)

Method Example

public class HelloWorld

{

public static void main(String args[])

{

//code

}

}

(9)

Method Example

public class HelloWorld

{

public static void main(String args[])

{

//code

}

}

(10)

Method Example

public class HelloWorld

{

public static void main(String args[])

{

//code

}

}

String args[] is the

type and name of

(11)

Other Examples

private int returnSum(int[] list)

void setValue()

(12)

Modifiers

Access control / Scope

Static

Final

Abstract

(13)

Access Control / Scope

Will discuss in more detail in future classes

Modifier

Class

Scope

Package

Scope

Subclass

Scope

Package Scope

Outside

public

TRUE

TRUE

TRUE

TRUE

(14)

Access Control / Scope

public

public void go(){

//code here

}

protected

protected void go(){

//code here

}

default

void go(){

//code here

}

private

(15)

Static

Will discuss in more detail in future classes

Whether or not the method is called by the

class or the object

Static methods do not need an instance of the

class to be created to be called

ClassName.staticMethod();

(16)

Final

Will discuss in more detail in future classes

Different than a final variable

(17)

Abstract

Will discuss in more detail in future classes

(18)

Synchronized

Will discuss in more detail in future classes

For multithreading, ensures that the method

(19)

Return Types

The type of value the function returns

Can be any object or primitive, or nothing

void – no return value

(20)

Return Types

public void returnNothing() {

}

public int returnInt() {

return 1; }

public int returnInt2() {

(21)

Parameters

Input values the method takes

Can be any object or primitive, or none

Comma separated list

Limit is 255

Generally only 3-4 is acceptable

(22)

Parameters

//no parameters

void doSomething(){}

//one integer parameter

void doSomething(int x){}

(23)

Method Signature

All methods within the same class must have a

unique method signature

Method signature

(24)

Example - SignatureExamples.java

SignatureExamples.java

(25)

Calling Methods

Must call methods with the expected

parameters

Methods can be called three ways

(26)

Calling Methods

Class name

Static methods are called with the class name

//calls the floor method of the Math class

Math

.floor(5.2);

(27)

Calling Methods

Object name

Regular methods are called with the object name

(28)

Calling Methods

No name

Methods in the same class can be called without

any name

(29)

Example – Method Calling

MethodCallExample.java

MethodCallExample2.java

(30)

Parameter Passing

Pass by Value

For primitive types

Makes a copy of the data and uses the copy

Pass by Reference

For object types and arrays

(31)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

(32)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

int b = increment(a); System.out.println(a); System.out.println(b);

(33)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

int b = increment(a); System.out.println(a);

(34)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

int b = increment(a); System.out.println(a); System.out.println(b);

Copy the value of

‘a’ into a new local variable

defined for the function which

(35)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

int b = increment(a); System.out.println(a);

(36)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

(37)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

int b = increment(a); System.out.println(a);

return a

a

6

(38)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

int b = increment(a); System.out.println(a);

System.out.println(b);

5

a

Store return

value in b

6

(39)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

int b = increment(a); System.out.println(a);

a

b

(40)

Pass by Value

public static int increment(int a) {

a++; return a; }

public static void main(String args[]) {

int a = 5;

(41)

Example – PassByValue.java

(42)

Pass by Reference

public static int[] increment(int[] a) {

a[0]++; return a; }

public static void main(String args[]) {

int a[] = {5, 10};

int[] b = increment(a);

System.out.println(a[0] + “ ” + a[1]); System.out.println(b[0] + “ ” + b[1]);

Set variable

a[0] = 5

a[1] = 10

(43)

Pass by Reference

public static int[] increment(int[] a) {

a[0]++; return a; }

public static void main(String args[]) {

int a[] = {5, 10};

int[] b = increment(a);

System.out.println(a[0] + “ ” + a[1]);

Call increment

with a

(44)

Pass by Reference

public static int[] increment(int[] a) {

a[0]++; return a; }

public static void main(String args[]) {

int a[] = {5, 10};

int[] b = increment(a);

System.out.println(a[0] + “ ” + a[1]); System.out.println(b[0] + “ ” + b[1]);

a

A new pointer is made that

points to the SAME memory

(45)

Pass by Reference

public static int[] increment(int[] a) {

a[0]++; return a; }

public static void main(String args[]) {

int a[] = {5, 10};

int[] b = increment(a);

System.out.println(a[0] + “ ” + a[1]);

increment the first value

6

, 10

a

(46)

Pass by Reference

public static int[] increment(int[] a) {

a[0]++; return a; }

public static void main(String args[]) {

int a[] = {5, 10};

int[] b = increment(a);

System.out.println(a[0] + “ ” + a[1]); System.out.println(b[0] + “ ” + b[1]);

return the array

6, 10

a

(47)

Pass by Reference

public static int[] increment(int[] a) {

a[0]++; return a; }

public static void main(String args[]) {

int a[] = {5, 10};

int[] b = increment(a);

System.out.println(a[0] + “ ” + a[1]);

Store the return value in b

6, 10

a

(48)

Pass by Reference

public static int[] increment(int[] a) {

a[0]++; return a; }

public static void main(String args[]) {

int a[] = {5, 10};

int[] b = increment(a);

System.out.println(a[0] + “ ” + a[1]); System.out.println(b[0] + “ ” + b[1]);

Print out array a

6, 10

a

(49)

Pass by Reference

public static int[] increment(int[] a) {

a[0]++; return a; }

public static void main(String args[]) {

int a[] = {5, 10};

int[] b = increment(a);

System.out.println(a[0] + “ ” + a[1]);

Print out array b

6, 10

a

(50)

Example – PassByReference.java

(51)

Method Overloading

Method with the same name but different

parameters

Assumed to have the same purpose

Useful when the same task can be done with a

different set of initial parameters

(52)

Example - OverloadExample.java

(53)

Variable Scope

Local Variable = variable defined within the

method

Scope starts at declaration

(54)

Example - SelectionSort.java

Week3

SelectionSort.java

What is the scope of unsortedArray?

What is the scope of i?

What is the scope of j?

What is the scope of totalSelectionSort?

References

Related documents

“If it is raining or the weather forecast is bad then I take an umbrella” Truth table: True True True True False True True True False False False False umbrella Bad forecast

// Dispenser returns true for // cash amount Dispenser.dispense(amount); Menu / get pin [false] [true] Check CheckMachine Cash [false] [false] [true] [true] [false] CheckAccount

bool true – function successfully executed, device was reset to default settings false – error in

31094 Portland, OR TRUE NW01330 FALSE FALSE FALSE TRUE FALSE

TRUE/FALSE Denmark is in Northern Europe TRUE/FALSE Spain is in Western Europe.. FALSE

 StartScriptEnabled (set to true, false by default)  StartScriptName (custom start script name).  NativeVersionEnabled (use shared OS libraries)  SecureListener (Enables SSL,

Do you want to return the number as a string ( TRUE) or as numeric (FALSE, the default).. decimals Do you want to include the possibility of decimal numbers ( TRUE) or not (FALSE,

Logical, if TRUE (FALSE by default) include the summary statistics across columns in a separated column.. colTotalLab String, label for the total column ’Total’