Procedures Student Lesson, 3.12 and 3.13

Procedure Algorithm / How Procedures Work

  • Remember that procedures are essentially a set of programming instructions, or lines of code, that accomplish a goal. When executed, each line of code is executed in order (step after step after step) to get to the goal.

Regular Code / Python

# Procedure called "applyTax" that applies a percent tax to a price
def applyTax(price, percentTax): # When writing a procedure, first decide what parameters you will need to accomplish your goal
    # Step 1: Calculate the amount taxed
    taxAmount = price * percentTax/100

    # Step 2: Add the amount taxed to the price to get the end amount
    endAmount = price + taxAmount

    return endAmount

# Use procedure to apply a 50% tax to a price of $10
cost = applyTax(10, 50)
print(cost)
15.0

CollegeBoard Pseudo-Code

  • Note that the pseudo-code below has the exact same purpose as the actual code above. Ignore the breaks and spaces since they are used for formatting.

    Differences between prseudo-code and Python:

  • Pseudo-code uses “PROCEDURE” instead of “def”
  • Pseudo-code uses “<–” instead of “=”
  • Pseudo-code uses “{}” instead of “:” to mark where a procedure starts and ends

Pseudo-code example

PROCEDURE applyTax (price, percentTax)
{
    taxAmount <– price * percentTax/100
    endAmount <– price + taxAmount
    return endAmount
}

Mini-Hack

Instructions:

  • The blue triangle represents a robot that moves in a grid of squares. The tip of the triangle indicates where the robot is facing.
  • Write a procedure that allows the robot to make a detour around a block by moving to the left.

Commands

  • MOVE_FORWARD() - Moves the robot forward one square
  • MOVE_BACKWARD() - Moves the robot backward one square
  • ROTATE_LEFT() - Rotates the robot 90 degrees left
  • ROTATE_RIGHT() - Rotates the robot 90 degrees right

Your code here:

Homework

Instructions

  • There are three total problems:
    1. An easy regular code (Python) problem
    2. A medium pseudo-code problem
    3. A hard regular code (Python) problem
  • Completing question 1 and 2 nets you 0.9/1 if you do it correctly. Completing or attempting question 3 or adding creativity will potentially raise you above 0.9.

Question 1

  • Write a procedure to apply a percent discount to a set price. See the example about applying tax if you’re stuck.
# Your code here

Question 2

  • Create your own robot problem! Include a picture with a square grid to represent the map and triangle to represent the robot. Add a flag to a square to represent the end-point and a shaded-in block to represent a detour. Write a procedure in pseudo-code to move the robot from the start, past the detour, and to the end point.

Add your image here by adding the link between the “” and removing the formatting for the comment:

# Your code here