3.12-3.13 Developing Procedures

What is a procedure?

A procedure is a named group of programming instructions

2 Main Types of Procedures:

  • Procedure that returns a value of data
  • Procedure that executes a block of statements

Learning Objective:

Be able to look at code and determine the purpose, and be able to write a procedure to manage complexity of program. understand the following terms and what they look like as well as how to use them in code

  • Procedure Parameters: variables that are defined within the procedure’s parentheses and serve as placeholders for values that can be passed to the procedure when it is called, allowing the procedure to operate on different data or values each time it is executed.
  • Procedure Algorithms: a step-by-step set of instructions that defines a specific task or operation to be performed within a computer program
  • Procedure Return Values: data or results that a procedure can send back to the part of the program that called it, allowing the procedure to provide information or perform a specific task and then share the outcome with the rest of the program.

Name, Parameters, Algorithm, Return Values, and Calling a Procedure

## Example of Procedure that Updates A Quiz Grade
def updatequiz(currentgrade, quizgrade):
    if quizgrade > currentgrade:
        currentgrade = quizgrade
    return currentgrade

currentgrade = 75
quizgrade = 88
currentgrade = updatequiz(currentgrade, quizgrade)
print(currentgrade)
88

Popcorn Hack #1:

Identify the name of the procedure below, tell us the purpose and parameters of the procedure and identify the algorithm return value:

  • Name: updateweather
  • Parameters:currentweather, weather
  • Algorithm (paste lines): def updateweather(currentweather, weather): if currentweather> weather: weather = currentweather print(“today is warmer than yesterday”) else: print(“today is colder than yesterday”) return currentgrade
  • Return Value: current weather
  • Calling Procedure Line: currentweather = 71 weather = 66 currentgrade = updateweather(currentweather, weather) print(“the temperature right now is”, currentweather, “degrees”)
def updateweather(currentweather, weather):
    if currentweather> weather:
        weather = currentweather
        print("today is warmer than yesterday")
    else:
        print("today is colder than yesterday")
    return currentweather

currentweather = 71
weather = 66
currentweather = updateweather(currentweather, weather)
print("the temperature right now is", currentweather, "degrees")
today is warmer than yesterday
the temperature right now is 71 degrees

Costructing Classes for Names & Data with Procedures

# Class Construction
class Short:
    name = ""
    height = 0

class Tall:
    name = ""
    height = 0

# Procedure to Classify as Short or Tall
def classify_person(name, height):
    if height < 70:
        short_person = Short()
        short_person.name = name
        return short_person
    else:
        tall_person = Tall()
        tall_person.name = name
        return tall_person

Class Definitions: The code defines two classes, “Short” and “Tall,” each having two attributes: name and height. These attributes can be used to store the name and height of individuals.

Classification Procedure: The classify_person function takes two parameters: name and height. Depending on the provided height, it creates an instance of either the “Short” or “Tall” class. It sets the name attribute for the person and returns the corresponding instance.

Popcorn Hack #2:

Use the example above to use a procedure to create two classes of your choosing. Create at least 2 objects and class them with your procedure

# Popcorn Hack 2
class smart: 
    name: ""
    grade = 0

class dumb:
    name: ""
    grade = 0
    
# Procedure to Classify as smart or dumb
def classify_person(name, grade):
    if grade < 70:
        dumb_person = dumb()
        dumb_person.name = name
        return dumb_person
    else:
        smart_person = smart()
        smart_person.name = name
        return smart_person
    
# Create objects and classify them as short or tall
person1 = classify_person("Thomas", 69)
person2 = classify_person("John", 76)
person3 = classify_person("Bob", 27)
person4 = classify_person("Timothy", 98)

# Display results for all four objects
for person in [person1, person2, person3, person4]:
    print(f"{person.name} is {'dumb' if person is dumb else 'smart'}.")
Thomas is smart.
John is smart.
Bob is smart.
Timothy is smart.

Calling Methods of an Object w/ Procedures

# Creating Classes
class Short:
    name = ""
    height = 0

class Tall:
    name = ""
    height = 0

#Procedure to classify as short or tall
def classify_height(name, height):
    if height < 70:
        short_person = Short()
        short_person.name = name
        return short_person
    else:
        tall_person = Tall()
        tall_person.name = name
        return tall_person

# Create objects and classify them as short or tall
person1 = classify_height("Nihar", 70)
person2 = classify_height("Will", 76)
person3 = classify_height("Jayden", 75)
person4 = classify_height("Howie", 70)

# Display results for all four objects
for person in [person1, person2, person3, person4]:
    print(f"{person.name} is {'Short' if person is Short else 'Tall'}.")


Nihar is Tall.
Will is Tall.
Jayden is Tall.
Howie is Tall.

HW Hacks!!

  1. Create a procedure that replaces a value with a new one (ex. update height)
  2. Create a procedure that constructs classes of your choosing and create at least 4 objects to be sorted into your classes. Call your procedure to display your objects.
# HW Hack 1
def updatepaycheck(currentpaycheck, changepaycheck):
    if currentpaycheck > changepaycheck:
        changepaycheck = currentpaycheck
        print("you have recieved less money then the last paycheck")
    else:
        print("you have recieved more or equal to your last paycheck")
    return currentpaycheck
currentpaycheck = 340
changepaycheck = 210
currentpaycheck = updatepaycheck(currentpaycheck, changepaycheck)
print("your paycheck is", currentpaycheck)
you have recieved less money then the last paycheck
your paycheck is 340
# Define a class for WeatherCondition
class WeatherCondition:
    def __init__(self, condition, temperature):
        self.condition = condition
        self.temperature = temperature

    def __str__(self):
        return f"{self.condition}, {self.temperature}°C"

# Define a class for Location
class Location:
    def __init__(self, city, country):
        self.city = city
        self.country = country

    def __str__(self):
        return f"{self.city}, {self.country}"

# Create weather objects with different locations
weather1 = WeatherCondition("Sunny", 25)
location1 = Location("LA")

weather2 = WeatherCondition("Cloudy", 18)
location2 = Location("London")

weather3 = WeatherCondition("Rainy", 12)
location3 = Location("Paris")

weather4 = WeatherCondition("Partly Cloudy", 20)
location4 = Location("Sydney")

# Create a list of weather objects and their corresponding locations
weather_objects = [
    (weather1, location1),
    (weather2, location2),
    (weather3, location3),
    (weather4, location4)
]

# Sort the weather objects by location
sorted_weather = sorted(weather_objects, key=lambda item: item[1].city)

# Display the sorted weather objects
for weather, location in sorted_weather:
    print(f"Weather in {location}: {weather}")

Weather in London, UK: Cloudy, 18°C
Weather in Los Angeles, USA: Sunny, 25°C
Weather in Paris, France: Rainy, 12°C
Weather in Sydney, Australia: Partly Cloudy, 20°C

Reflection:
Developing Procedures:

  • Procedures are like step-by-step instructions for solving problems.

  • I learned to break down complex tasks into smaller, manageable steps.

  • Clear procedures help in organizing and optimizing code.

  • Error handling is essential to address unexpected issues.

  • Proper documentation is key for team collaboration and future reference.

  • Version control ensures procedures can be tracked and improved.

  • Developing efficient procedures is a fundamental skill in coding.

  • Procedures bring consistency and reliability to software development.

  • I discovered that procedures can be reused for similar tasks, saving time.

  • An iterative approach allows for refining procedures over time.

  • In summary, developing procedures in computer science involves creating clear, reusable, and efficient sets of instructions to solve problems and improve software development practices.