3.12

Calling procedures

Key Vocabulary

  • Procedure:a named group of programming instructions that may have parameters and return values- Parameters: input values of a procedure
  • Arguments: specify the values of the parameters when a procedure is called

Big Ideas

  • Procedures can be referred to as method or function depending on the programing language.
  • A procedure call interrupts an execution of statements and makes the program execute the statements in the procedure.
  • If you have a set of statements and inside there is a procedure then when it reaches that procedure it will execute that procedure and then go back to the rest of the statments.
  • If you see a return statement you automatically end the procedure even if there is more statements
  • Procedures are that a catch-all term for codes used to identify what was done to or given to a patient
  • If you have a set of statements in a procedure the statement will go thought the top of the procedure to the bottom in order.

Practice

  1. Here is a practice problem that involves calling a procedure by having an input and an output. This is a apply discount and apply tax procedure.
    • $80 item receives a 20% discount and a tax is 8%
    • PROCEDURE applyDiscount (cost, percentDiscounted) { temp <-- 100 - percentDiscounted
temp <- temp / 100
cost <- cost * temp

Print(cost)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/Users/jesa/vscode/andafp/_notebooks/2022-12-07-Lesson312and313.ipynb Cell 3 in <cell line: 1>()
----> <a href='vscode-notebook-cell:/Users/jesa/vscode/andafp/_notebooks/2022-12-07-Lesson312and313.ipynb#W3sZmlsZQ%3D%3D?line=0'>1</a> temp <- temp / 100
      <a href='vscode-notebook-cell:/Users/jesa/vscode/andafp/_notebooks/2022-12-07-Lesson312and313.ipynb#W3sZmlsZQ%3D%3D?line=2'>3</a> cost <- cost * temp
      <a href='vscode-notebook-cell:/Users/jesa/vscode/andafp/_notebooks/2022-12-07-Lesson312and313.ipynb#W3sZmlsZQ%3D%3D?line=4'>5</a> Print(cost)

NameError: name 'temp' is not defined
  1. Temperature Procedure
  • This procedure is used to Convert Fahrenheit to Celsius

  • What is 80 degrees in Celsius?

  • PROCEDURE convert Fahrenheit (temperature) {

Celsius <- tempature - 32
Celsius <- Celsius * 5/9

Print (Celsius)

3.13

Developing procedures, to manage a program and the complexity

Key Vocabulary

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

Big Ideas

  • There are two types of procedures, one that returns a value or some type of data and on that just executes a block of statements
  • Procedure-is a named group of programming instructions that serves a purpose
  • How to code Procedures-While Coding a procedure you must makes sure it has as name and a clear purpose weather to execute a response or complete an action
  • One common type of abstraction is procedural abstraction, which provides a name for a process and allows a procedure to be only knowing what it doe,not how it does it.
  • The subdivision of a computer program into separate subprograms is called modularity
  • It is important to understand you procedures procedure does when creating one as it will help you create a impactful procedure

Creating a Procedure

  1. Pick a descriptive name
  2. See if you need any parameters for this procedure (what data do you need to accomplish my goal? What king of information am I going to need?)
  3. 2 of the parameters in the example below could be the quizGrade and currentPoints (need this data)

Practice

  1. PROCEDURE updateGRADE(points, current grade,new grade){

    • currentGrade <-- current points/total points
    • currentGrade <-- current grade * 100
    • if (currentGrade > quizGrade){ quizGrade <-- currentGrade }
    • return (quizGrade) }
  2. after you take a quiz your teacher lets you retake it for full credit in which your are able to replace your grade TEST WITH first quiz GRADE is 40% and second is 90%

PROCEDURE updateGRADE(points, current grade,new grade){
    currentGrade = current points /75
    currentGrade = current grade * 100
    if currentGrade > quizGrade
    return currentGrade }
  Input In [3]
    PROCEDURE updateGRADE(points, current grade,new grade){
              ^
SyntaxError: invalid syntax

Hackathon!

Procedure winner(){
    If Can_Move(Forward)
        Move_Forward()
    Else Rotate_Left()
        Move_Forward()
        If Can_Move(Right)
            Rotate_Right()
            Move_Forward()
}
counter = 1
while counter <= 2:
    Procedure maze(){
        If: Can_Move(Forward)
            Move_Forward()
        Else: Rotate_Right()
            Move_Forward()
            If Can_Move(Left)
                Rotate_Left()
                Move_Forward()
    }
    if i == 2
        break
    Procedure top(){
    If Can_Move(Left)
        Rotate_Left()
        Move_Forward()
    }

Homework Hacks

3.12 Part 1

  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) }

  1. 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?

True, because it does not have DISPLAY in the procedure

False

  1. 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 Part 2

  1. What is a?
  • a -- ?
  • b -- ?
  • c -- 9
  • PROCEDURE find a () { b <-- 9 9
  • a <-- b c
  • Print (a) }

  • Answer: a is 9

  1. What is the cost?
  • cost ⟵ 173 tax - 10% PROCEDURE applytax (cost, percentDiscounted){

    • temp <-- 100 + percentTaxed
    • temp <-- temp / 100
    • cost <-- cost x temp
    • Print(cost)}
  • Answer: The cost $190.30

  1. What is the celsius value?
  • Temperature - 103 Degrees
  • PROCEDURE convert Fahrenheit (temperature) {
    • Celsius <-- temperature - 32
    • Celsius <-- Celsius x 5/9
  • Print (Celsius)}

  • Answer: 39.4 Celsius

3.13 Parts 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
    • Necessary Parameters: toprbyardspg(100), currentrbyards(1260), totalGames(12)
  • PROCEDURE replaceTopRb(toprbyardspg(100), curerntrbyards(1260), totalgames){
    • If (currentRbYards(1260) > toprbyardspg(100)){
      • toprbyardspg <-- currentRbYards
    • }
  • }
  1. Write a procedure that will allow the A+ to get to the 1, while avoiding the black boxes.

    • PROCEDURE getAplus(){
      • If (can_MoveForward):
        • Move_Forward
      • Else (can_MoveRight):
        • Rotate_Right
        • Move_Forward
        • If (can_MoveLeft);
          • Rotate_Left
          • Move_forward
    • }
  2. Which Is the Correct Way to define the Name of a Procedure?

  • A. PROCEDURE MYLIST
  • B. PROCEDURE MyList
  • C. procedure mylist

  • Answer: B. because procedure should be in all capitals and part of the the declared procedure should be capitalized, not all and not none.

  1. Write A Procedure that gets the Santa to the Christmas Tree
  • PROCEDURE SantaToTree{ count = 0
    • Move_Forward
    • Rotate_Left
    • Move_Forward( until count = 6)
      • break
    • Rotate_Left
    • Move_Forward }

What I have learned:

  • I learned the basics in creating a procedure:
    • that the name needs to be simple but make sense
    • that you can use for and while loops and iteration to shorten the code
    • Procedure in the code should be written in all caps like "PROCEDURE"
  • In algorithms:
    • there are many different algorithms to reach a certain solution
    • you can also use for and while loops and iteration to shorten algorithms