Notes

  • Procedures can perform certain operations but do not return values
  • Procedures can be used to convert values
  • The name of the procedure must be descriptive
  • We can modularize procedures by dividing complex procedures into several simple procedures

3.12 Hacks part 1

Problem 1: This problem involves parameters Qais is writing code to calculate formulas from his math class. He's currently working on a procedure to calculate average speed, based on this formula:

Average speed=

Total Time/Total Distance​

Highlight which of these is the best procedure for calculating and displaying average speed. PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (distance/time) } PROCEDURE calcAvgSpeed (distance) { DISPLAY (distance/time) } PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (time/distance) }

Problem 2: Procedures with return values James Hunter is looking through his classmate's program and sees a procedure called heightenEmotions: PROCEDURE heightenEmotions(myEmotion)

{ moreEnergy ← CONCAT(myEmotion, "!!!")

moreVolume ← UPPER(moreEnergy)

RETURN moreVolume }

That procedure manipulates strings using two built-in procedures, CONCAT for concatenating two strings together, and UPPER for converting a string to uppercase.

James Hunter then sees this line of code:

heightenEmotions("im mad")

After that line of code runs, will nothing be displayed?

False

Problem 3: Procedures with return values Bubz is writing a program to calculate the carbon footprint of his activities. The procedure calcFlightFootprint calculates the pounds of carbon dioxide produced per passenger in a flight that covers a given number of miles and seats a given number of passengers.

PROCEDURE calcFlightFootprint(numMiles, numPassengers) { CO2_PER_MILE ← 53.29

carbonPerFlight ← numMiles * CO2_PER_MILE

carbonPerPassenger ← carbonPerFlight / numPassengers

RETURN carbonPerPassenger

}

Bubz wants to use that procedure to calculate the total footprint for his two upcoming flights: LA to NY: 2,451 miles and 118 passengers NY to London: 3,442 miles and 252 passengers

Which of these code snippets successfully calculates and stores her total footprint? Highlight 2 answers.

totalFootprint ← calcFlightFootprint(2451, 118) + calcFlightFootprint(3442, 252)

laNyCarbon ← calcFlightFootprint(2451, 118) nyLondonCarbon ← calcFlightFootprint(3442, 252) totalFootprint ← laNyCarbon + nyLondonCarbon

</div> </div> </div> </div>

3.12 Hacks part 2

PROCEDURE find a ()

{ c -- 9

b <-- 9 * 9

a <-- b * c

Print (a)

}

What is a?

a=729

cost ⟵ 173 tax - 10%

PROCEDURE applytax (cost, cpercentDiscounted) { temp <-- 100 + percentTaxed

temp <-- temp / 100

cost <-- cost x temp

Print(cost)}

What is the cost?

cost is 190.3

Tempature - 103 Degrees

PROCEDURE convet Fahrenheit (tempature)

{

Celsius <-- tempature - 32

Celsius <-- Celsius x 5/9

Print (Celsius)}

Celsius is 39.44

3.13 Homework/Hacks for Part 1 and Part 2 of the lesson

  1. Create a procedure that is meant to replace the top running backs yards per game in one season if the current running back has more yards per game

Necessary Parameters: toprbyardspg(100), currentrbyards(1260), totalGames(12)

PROCEDURE updateByardspg(toprbyardspg, currentrbyards, totalGames)
{
    currentrbyardspg <-- currentrbyards/totalGames
    if (currentrbyardspg > toprbyardspg)
    {
        toprbyardspg <-- currentrbyards
    }
    return(toprbyardspg)
}
  1. Write a procedure that will allow the A+ to get to the 1, while avoiding the black boxes.

Use what you learned about moving robots

PROCEDURE A+GetTo1()
{
    move_Forward()
    rotate_left()
    move_Forward()
    move_Forward()
    rotate_right()
    move_Forward()
    move_Forward()
    move_Forward()
    rotate_left()
    move_Forward()
    move_Forward()
    rotate_left()
    move_Forward()
    move_Forward()
    rotate_right()
    move_Forward()
    rotate_left()
    move_Forward()
    move_Forward()
}
  1. Which Is the Correct Way to define the Name of a Procedure?

A. PROCEDURE MYLIST

B. PROCEDURE MyList

C. procedure mylist

B

  1. Write A Procedure That gets the BeachBall To the Green Square
PROCEDURE GetBeachballToGreen()
{
    move_Forward()
    rotate_left()
    move_Forward()
    rotate_right()
    move_Forward()
    move_Forward()
    move_Forward()
    move_Forward()
    move_Forward()
    rotate_left()
    move_Forward()
    move_Forward()

}