Tasks and Task Management : --Time-critical tasks --Contending tasks --Communicating tasks.
Neal Stublen [email protected]. Breaking Down Tasks Reduce large tasks into smaller units of code ...
-
Upload
benedict-holt -
Category
Documents
-
view
214 -
download
0
Transcript of Neal Stublen [email protected]. Breaking Down Tasks Reduce large tasks into smaller units of code ...
USING METHODS
Breaking Down Tasks
Reduce large tasks into smaller units of code
Smaller units of code might be called any of the following:ProcedureFunctionSubroutineMethod
Why use methods?
Provides high level perspective Play MP3
Open fileRead file contentsDecode file contentsSend decoded content to sound card
We say methods encapsulate details
Why use methods?
Smaller tasks are generally easier to understand
Smaller tasks can be reused Examples:
Verify a dateAuthenticate a userSpell check a wordCalculate the area of a circlePlay an alarmTake a picture
Naming Methods
Methods encapsulate behavior on specific data
Usually named with a verb-noun combinationvalidateDate()authenticateUser()spellCheckWord()calculateArea()playAlarm()takePicture()
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
Example Method
main()
displayAddress()
return
displayAddress()
output "S. Holmes"
output "221 B Baker Street"
output "London, England"
return
Declaring Variables We've looked at declaring variables:
type identifier = valuenum counter = 0string 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
Local Variablesclass 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
Class Variablesclass 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
Global Variablesstring 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
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
Local Variable Scopeclass Program
main()
displayPerson()
displayPartner()
return
displayPerson()
string name = "S. Holmes"
output name
return
displayPartner()
string name = "J. Watson"
output name
return
endClass
Method Parameters When we need to provide information to a
method, we use method parametersvalidateDate(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
Parameters by Valuemain()
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
Parameters by Referencemain()
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
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
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
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
Using a Return Valuevoid 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
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.
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
"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
Recursive Methods
How would you implement a factorial function?5! = 5 * 4 * 3 * 2 * 15! = 5 * 4!4! = 4 * 3!, etc.
Some methods can be designed to call themselves
These methods implement recursion
Factorialnum 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
Summing an Arraynum 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
Summary
Creating methods Local variables and scope Method parameters Return values Overloading methods Predefined methods Recursive methods