neal stublen [email protected]. breaking down tasks reduce large tasks into smaller units of code ...

28
PROGRAMMING FUNDAMENTALS Neal Stublen [email protected]

Upload: benedict-holt

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

PROGRAMMING FUNDAMENTALS

Neal Stublen

[email protected]

Page 2: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

USING METHODS

Page 3: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

Breaking Down Tasks

Reduce large tasks into smaller units of code

Smaller units of code might be called any of the following:ProcedureFunctionSubroutineMethod

Page 4: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 5: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 6: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

Naming Methods

Methods encapsulate behavior on specific data

Usually named with a verb-noun combinationvalidateDate()authenticateUser()spellCheckWord()calculateArea()playAlarm()takePicture()

Page 7: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 8: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

Example Method

main()

    displayAddress()

return

displayAddress()

    output "S. Holmes"

    output "221 B Baker Street"

    output "London, England"

return

Page 9: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 10: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 11: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 12: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 13: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 14: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 15: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 16: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 17: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 18: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 19: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 20: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 21: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 22: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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.

Page 23: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 24: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

"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

Page 25: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 26: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 27: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

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

Page 28: Neal Stublen nstublen@jccc.edu. Breaking Down Tasks  Reduce large tasks into smaller units of code  Smaller units of code might be called any of the

Summary

Creating methods Local variables and scope Method parameters Return values Overloading methods Predefined methods Recursive methods