Lecture 10: Methods - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Lecture 10: Methods

Description:

A method is a chunk of code that can be executed by calling its name. ... As we've seen, a method can return a value that it computed. ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 13
Provided by: stephenr9
Category:

less

Transcript and Presenter's Notes

Title: Lecture 10: Methods


1
Lecture 10 Methods
  • Yoni Fridman
  • 7/17/01

2
Outline
  • Overview
  • Method format
  • Arguments
  • The returned value
  • Variable scope
  • Using methods
  • Commenting methods
  • The method main()

3
Overview
  • Methods provide a way to break a program into
    smaller chunks.
  • Youve already seen many examples
  • System.out.println(Hello)
  • Math.sqrt(25.0)
  • playerWantsToHit()
  • getNewCard()
  • A method is a chunk of code that can be executed
    by calling its name.
  • But where is the code thats executed when a
    method is called?

4
Method Format
public static int triple(int n) n 3
return n
5
Arguments
  • A method can have any number of arguments.
  • getNewCard() takes no arguments.
  • System.out.println(Hello) takes 1 argument.
  • Math.pow(3, 2) takes 2 arguments.
  • Etc.
  • When an argument of type int, double,
    or boolean is passed to a method, the
    method makes a copy.
  • Strings and arrays work differently. More
    later.

public static int triple(int n) n 3
return n
int n 5 int nTripled triple(n) System.out.pr
intln(nTripled) System.out.println(n)
6
The Returned Value
  • As weve seen, a method can return a value that
    it computed.
  • getNewCard() returns a random value between 1 and
    10.
  • Math.pow(x, y) returns xy.
  • The type of the returned value must be stated.
  • The return statement must be included
    at the end of the method body this
    indicates the value being returned.

public static int triple(int n) n 3
return n
7
The Returned Value
  • A method can do stuff, but then return nothing.
  • Example System.out.println(Hello)
  • In this case, the return type is void, and
    there doesnt have to be a return statement.

public static void showTriple(int n) n
3 System.out.println(n)
8
Variable Scope
  • Variables declared inside a method have local
    scope. That is, they only exist inside the
    method.
  • What will happen if you try to use
    nTimes3 outside of the method
    triple()?
  • Error!
  • Also, as soon as the method ends, the
    local variable is lost.

public static int triple(int n) int nTimes3
n 3 return nTimes3
int n 5 int nTripled triple(n) System.out.pr
intln(nTimes3)
9
Using Methods
  • Why use methods?
  • So the same code doesnt have to be repeated.
  • Example getNewCard()
  • To make the program easier to read.

initializePlayerHands() while (playerHits)
drawNewCard() checkIfBusted() while
(dealerTotal lt 17) drawNewCard() determineWin
ner()
10
Commenting Methods
  • Write a comment block right before each method,
    which includes three parts
  • Method description What the method does.
  • Input A description of the arguments passed to
    the method.
  • Output A description of the value being
    returned from the method.

// This method triples the value of an int. //
Input n any int. // Output An int that
equals three times the input. public static int
triple(int n) n 3 return n
11
The Method main()
  • Now we can understand (almost) this line of code
  • public static void main(String args)
  • Its a method called main that takes in an
    array of Strings and doesnt return anything.
  • When you execute a program (by hitting the
    button), that tells Java to execute the main()
    method.

12
Homework
  • Read 3.1 3.5.
  • HW5 out today, due Monday.
  • Poker.
Write a Comment
User Comments (0)
About PowerShow.com