- CB 3.12,3.13 Developing Procedures

What is a procedure?

A procedure is a named group of code that has paramaters and return values. Procedures are known as methods or functions depending on the language.

A procedure executes the statements within it on the parameters to provide a return value.

What are parameters?

Paramaters are input values of a procedure that are specified by arguments.Arguments specify the values of the parameters when a procedure is called.

By creating theses algorithms the readibility of code increases and the complexity decreases. This is becasue a function’s name can tell the reader what action it will perform, and by calling it, the code becomes more clean and easy to understand.

What is a return value?

A return value is the value that is returned when a function or a method is called.

That return value can be assigned or printed

Procedures are used to create algorthims that can perform certain actions or return values. When a procedure returns a value, theis information must be stored in a variable for later use. However some procedures like the MOVE_FORWARD() perform an action, and don’t return a value. The image above provides an example of where procedures that don’t output a value would be used.

A 60$ item recieves a 20% discount and taxed at 8%.
PROCEDURE applyDiscount(cost, percentDiscounted)
{
    temp  100 - percentDiscounted
    temp temp/ 100
    cost  cost *temp
    RETURN(cost)
}

price  applyDiscount(60, 20)
This is how we get the final price with the discount by calling the procedure and assigning it to the price variable.


PROCEDURE applyTax(cost, percentTaxed)
{
    temp  100 + percentTaxed
    temp temp/ 100
    cost  cost *temp
    RETURN(cost)
}
price  applyTax(price, 8)
This applys the 8% tax to the price determined after the discount.

Popcorn Hack 1

Given the applyTax procedure above: How would you call the procedure to get it to find the price using cost = 50, and percentTaxed = 10, and what value will it return?

bravo = input("what do you want")
print(bravo)
easy popcorn hacks

What Are Functions?

What Are The Components of a Function?

# Defining Functions
#
# def function_name(parameter1, parameter2, etc..):
#     code here...
#
#     return return_value;

# return the value of parameter1 plus parameter2;
def add(parameter1, parameter2): # creates a function that takes in two parameters
    solution = parameter1 + parameter2; # sets solution to the sum of parameter1 and parameter2
    return solution; # return solution
    
print(add(5, 5)); # prints the return value of add(5,5)

Popcorn Hack 2:

1. Make a function that returns the difference of two numbers

parameter1 = int(input("input a number"))
parameter2 = int(input("input a second number"))

def add(parameter1, parameter2):
    solution = parameter1 + parameter2
    return solution; 

print(add(parameter1, parameter2))
7

What is a Class?

How Does a Class Work?

# Defining Classes
class person:
    def __init__(self, name, age, ): # constructor
        self.name = name;
        self.age = age;
    
    def getName(self): # method to create get name
        return self.name;
    
    def getAge(self): # method to create get age
        return self.age;
    
    def setName(self, name): # method to create set name
        self.name = name;
        
    def setAge(self, age): # method to create set age
        self.age = age;
        
    def yearOlder(self): # method to increment age by 1
        self.age += 1;
        
    def __str__(self): # method that returns a string when the object is printed
        return (f"My name is {self.name} and I am {self.age} years old.")

Person1 = person("John Doe", 15);
print(Person1)


print(Person1);

Popcorn Hack 3:

1. Create a Car class which has the attributes model, vehicle name, and price

2. Create instances of the following cars

#popcorn hack #3

class Car:
    def __init__(self, name, model_year, price):
        self.name = name
        self.model_year = model_year
        self.price = price

# Creating instances of the specified cars
honda_civic = Car("Honda Civic", 2018, 13000)
toyota_prius = Car("Toyota Prius", 2023, 28000)
chevrolet_impala = Car("Chevrolet Impala", 2020, 22000)

# Printing the car information
print("Car 1:")
print(f"Name: {honda_civic.name}")
print(f"Model Year: {honda_civic.model_year}")
print(f"Price: ${honda_civic.price}")
print()

print("Car 2:")
print(f"Name: {toyota_prius.name}")
print(f"Model Year: {toyota_prius.model_year}")
print(f"Price: ${toyota_prius.price}")
print()

print("Car 3:")
print(f"Name: {chevrolet_impala.name}")
print(f"Model Year: {chevrolet_impala.model_year}")
print(f"Price: ${chevrolet_impala.price}")
Car 1:
Name: Honda Civic
Model Year: 2018
Price: $13000

Car 2:
Name: Toyota Prius
Model Year: 2023
Price: $28000

Car 3:
Name: Chevrolet Impala
Model Year: 2020
Price: $22000

Homework:

Assignment 1: How do you use functions?

Create a turtle python function that...

  1. Takes a single parameter as the number of sides
  2. Outputs a shape corresponding to the number of sides
  3. Call the function with the argument being a variable with the user input
  4. ```python # homework assignment #1 # checked with your team and told to leave this comment import turtle # Initialize the Turtle Graphics window turtle.setup(400, 400) pen = turtle.Turtle() def draw_square(): for _ in range(4): pen.forward(50) # Move forward by 50 units pen.right(90) # Turn right by 90 degrees # Call the draw_square function to draw a square draw_square() turtle.done() # Finish drawing and close the window when don ```

    Assignment 2:

    Create a student class that...

    1. Has a constructor that takes three parameters as attributes
      • email
      • name
      • grade
    2. Three getter methods to access the name, email, and grade
    3. Three setter methods to modify the name, email, and grade
    4. A to string method that returns the three instance variables in this format - "My name is {name}. My email is {email}. My grade is {grade}
    5. Create an instance of the class that corresponds with you
    ```python # homework assignment #2 class Student: def __init__(self, email, name, grade): self.email = email self.name = name self.grade = grade # Getter methods def get_email(self): return self.email def get_name(self): return self.name def get_grade(self): return self.grade # Setter methods def set_email(self, email): self.email = email def set_name(self, name): self.name = name def set_grade(self, grade): self.grade = grade # to_string method def to_string(self): return f"My name is {self.name}. My email is {self.email}. My grade is {self.grade}" # Create instances of the Student class ronit = Student("ronit@ronit.com", "Ronit", 95) vance = Student("vance@vance.com", "Vance", 88) jared = Student("jared@jared.com", "Jared", 92) # Accessing and printing the information of each student print("Ronit's Info:") print("Name:", ronit.get_name()) print("Email:", ronit.get_email()) print("Grade:", ronit.get_grade()) print(ronit.to_string()) print("\nVance's Info:") print("Name:", vance.get_name()) print("Email:", vance.get_email()) print("Grade:", vance.get_grade()) print(vance.to_string()) print("\nJared's Info:") print("Name:", jared.get_name()) print("Email:", jared.get_email()) print("Grade:", jared.get_grade()) print(jared.to_string()) ``` Ronit's Info: Name: Ronit Email: ronit@ronit.com Grade: 95 My name is Ronit. My email is ronit@ronit.com. My grade is 95 Vance's Info: Name: Vance Email: vance@vance.com Grade: 88 My name is Vance. My email is vance@vance.com. My grade is 88 Jared's Info: Name: Jared Email: jared@jared.com Grade: 92 My name is Jared. My email is jared@jared.com. My grade is 92