Download presentation
Presentation is loading. Please wait.
Published byBenedict Holt Modified over 9 years ago
1
Neal Stublen nstublen@jccc.edu
3
Breaking Down Tasks Reduce large tasks into smaller units of code Smaller units of code might be called any of the following: Procedure Function Subroutine Method
4
Why use methods? Provides high level perspective Play MP3 Open file Read file contents Decode file contents Send decoded content to sound card We say methods encapsulate details
5
Why use methods? Smaller tasks are generally easier to understand Smaller tasks can be reused Examples: Verify a date Authenticate a user Spell check a word Calculate the area of a circle Play an alarm Take a picture
6
Naming Methods Methods encapsulate behavior on specific data Usually named with a verb-noun combination validateDate() authenticateUser() spellCheckWord() calculateArea() playAlarm() takePicture()
7
Program Execution When calling a method, program execution jumps to the statements within the method When the method returns, program execution continues after the method call
8
Example Method main() displayAddress() return displayAddress() output "S. Holmes" output "221 B Baker Street" output "London, England" return
9
Declaring Variables We've looked at declaring variables: type identifier = value num counter = 0 string name = "John Smith" When we declare variables within a method, we say they are local to the method or local variables They can only be accessed from within the method When the method is executing, they are in scope; when the method returns they go out of scope
10
Local Variables class Program main() displayAddress() return displayAddress() string name = "S. Holmes" string address1 = "221 B Baker Street" string address2 = "London, England" output name output address1 output address2 return endClass
11
Class Variables class Program string name = "S. Holmes" string address1 = "221 B Baker Street" string address2 = "London, England" main() displayAddress() return displayAddress() output name output address1 output address2 return endClass
12
Global Variables string name = "S. Holmes" string address1 = "221 B Baker Street" string address2 = "London, England" class Program main() displayAddress() return displayAddress() output name output address1 output address2 return endClass
13
Class vs. Global The book uses the term "global" to refer to variables that are declared within a class and available to all methods within the class It will be much better to refer to these as "class" variables The term "global" should be used to refer to variables that are accessible from any code in an application Global variables are often considered unwise If you refer to class variables as global variables, you will very likely be misunderstood
14
Local Variable Scope class Program main() displayPerson() displayPartner() return displayPerson() string name = "S. Holmes" output name return displayPartner() string name = "J. Watson" output name return endClass
15
Method Parameters When we need to provide information to a method, we use method parameters validateDate(string date) authenticateUser(string user, string password) spellCheckWord(string word) playAlarm(num seconds) We send an argument to the method The method receives values in parameters Most likely you'll hear these terms used interchangeably
16
Parameters by Value main() num count = 6 doubleValue(count) output count // displays "6" return doubleValue(num value) // value contains a copy of count // changing the copy does not change count value = value * 2 output value // displays "12" return
17
Parameters by Reference main() num count = 6 doubleValue(count) output count // displays "12" return doubleValue(ref num value) // value refers to the variable count // value is just another name for count value = value * 2 output value // displays "12" return
18
Array Parameters The memory address of the first element is passed If a method changes an element, it is actually changing the original array contents Array parameters are passed by value, but may appear to be passed by reference
19
Returning a Value Methods can return a value to the caller by declaring a return type num calculateArea(num radius) num area = PI * radius * radius return area string requestName() string name output "Please enter your name." input name return name
20
Not Returning a Value We specify a void return type if the method does not return a value void main() return void playAlarm() play beep return
21
Using a Return Value void main() // We can store the return type and use it later num volume = calculateVolume(3, 6) output volume return num calculateArea(num radius) num area = PI * radius * radius return area num calculateVolume(num radius, num height) // We can use the return type without storing it num volume = calculateArea(radius) * height return volume
22
Guess the Pattern What logic is necessary to have a user guess a random ordering of the numbers 1, 2, 3, and 4? After each guess, the user will be told how many numbers are in the correct location.
23
Overloading Methods Some languages allow multiple methods with the same name, but different parameters The method is overloaded num calculateSeconds(num minutes) num seconds = minutes * 60 return seconds num calculateSeconds(num hours, num minutes) num seconds = (hours * 60 + minutes) * 60 returnseconds
24
"Predefined" Methods Most languages and platforms provide a library of commonly used methods Accessing a file system Receiving input from a keyboard or mouse Writing information to a display Complex mathematical operations
25
Recursive Methods How would you implement a factorial function? 5! = 5 * 4 * 3 * 2 * 1 5! = 5 * 4! 4! = 4 * 3!, etc. Some methods can be designed to call themselves These methods implement recursion
26
Factorial num factorial(num value) num f = 1 for index = 2; index < value; index += 1 f = f * index endfor return f num factorial(num value) num f = 1 if value <> 0 f = f * factorial(value – 1) endif return f
27
Summing an Array num sumArray(num[] values, num length) num sum = 0 for index = 0; index < length; index += 1 sum = sum + values[index] endfor return sum num sumArray(num[] values, num length) num sum = 0 if length <> 0 sum = sum + sumArray(values, length – 1) sum = sum + values[length – 1] endif return sum
28
Summary Creating methods Local variables and scope Method parameters Return values Overloading methods Predefined methods Recursive methods
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.