Notes

Vocab

  • A procedure is a named group of programming instructions that may have parameters and return values.
  • Parameters are input values of a procedure.

Notes on Procedures and Parameters

Procedures:

  • a method or function
  • should be descriptive

Parameters:

  • interrupts an execution of code

Vocab

  • Modularity - the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program

How to make procedures purposeful

  • should have multiple functions
  • Should be simple and get final result
  • be conscice

Vocab

  • Modularity - the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program

Hacks

3.12 Pt 1.

Skip Question 1

2.

The answer is PROCEDURE calcAvgSpeed (distance, time) { DISPLAY (time/distnace)}.

3.

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?

True, because the procedure does not display due to their being no code telling the procedure to display.

4.

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.

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

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

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

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

3.12 Pt. 2

1.

What is a?

A is 729 because c is 9 and b is 9*9 which is 81 and if a is b * c then 81 * 9 = 729.

3.13 Pt 1 and 2

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.

Given necesities: toprbyardspg(100), currentrbyards(1260), totalGames(12)

My Answer:

PROCEDURE replaceTopRb(toprbyardspg(100), currentrbyards(1260), totalGames(12)) { - If (currentRbYards(1260) > toprbyardspg(100)){ - toprbyardspg <– currentRbYards - } }

2.

  • PROCEDURE getAplus(){
    • If (can_MoveForward):
      • Move_Forward
    • Else (can_MoveRight):
      • Rotate_Right
      • Move_Forward
    • If (can_MoveLeft);
      • Rotate_Left
      • Move_forward
  • }

3.

Which Is the Correct Way to define the Name of a Procedure?

B, because the name of the procedure should not be in all caps, but PROCEDURE should be in all caps.

4.

Write A Procedure That gets the BeachBall To the Green Square

  • PROCEDURE getAplus(){
    • If (can_MoveForward):
      • Move_Forward
    • Else (can_MoveRight):
      • Rotate_Right
      • Move_Forward
    • If (can_MoveLeft);
      • Rotate_Left
      • Move_forward
  • }