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

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

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

# Code here
def minus(parameter1, parameter2):
    solution = parameter1 - parameter2;
    return solution;
print(minus(10,56));
def multiply(parameter1, parameter2):
    solution = parameter1 * parameter2;
    return solution;
print(multiply(10,600));
def divide(parameter1, parameter2):
    solution = parameter1 / parameter2;
    return solution;
print(divide(14,600));
-46
6000
0.023333333333333334

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

class Car: # Changed class name to start with an uppercase letter (convention)
    def __init__(self, model, vehicle, price): # constructor
        self.model = model;
        self.vehicle = vehicle;
        self.price = price;
        
    def getModel(self): # method to create get name
        return self.model;
    
    def getVehicle(self): # method to create get name
        return self.vehicle;
    
    def getPrice(self): # method to create get name
        return self.price;
    
    def setModel(self, model): # method to create set name
        self.model = model;
        
    def setVehicle(self, vehicle): # method to create set name
        self.vehicle = vehicle; # Fixed the typo here
        
    def setPrice(self, Price): # method to create set name
        self.price = Price;
        
    def __str__(self): # method that returns a string when the object is printed
        return (f"The car model is {self.model} and it is a {self.vehicle} and it costs {self.price} dollars.")
    
Car1 = Car("a Honda Civic", 2018, 13000); # Changed commas to periods

Car2 = Car("a Toyota Prius", 2023, 28000); # Changed commas to periods

Car3 = Car("a Chevrolet Impala", 2020, 22000); # Changed commas to periods

Car4 = Car("a ")
print(Car1);
print(Car2);
print(Car3);
The car model is a Honda Civic and it is a 2018 and it costs 13000 dollars.
The car model is a Toyota Prius and it is a 2023 and it costs 28000 dollars.
The car model is a Chevrolet Impala and it is a 2020 and it costs 22000 dollars.

assignment 1: Create a function that takes in an array as the parameter and returns the array of distinct values. DON’T USE SETS. TEST ARRAY: arr1 = [2,1,3,2,0,2,0,0,4,2,0,0,0,2,0,0,1,2,3,0,7,4,5,2,1,2,3,4,6]

def distinct_values(arr):
    distinct_arr = []
    for item in arr:
        if item not in distinct_arr:
            distinct_arr.append(item)
    return distinct_arr
arr1 = [2,1,3,2,0,2,0,0,4,2,0,0,0,2,0,0,1,2,3,0,7,4,5,2,1,2,3,4,6]
result = distinct_values(arr1)
print(result)
[2, 1, 3, 0, 4, 7, 5, 6]
import turtle

pen = turtle.Turtle(); # pen is the instance of Turtle which has methods that do certain actions

# Necessary methods:
# .forward(50) - moves the pen forward 50 units
# .right(angle) - turns the pen angle degrees right   
# OR
# .left(angle) - turns the pen angle degrees left

def shape(sides):
    #code here

numsides = input('How many sides do yoUUUU wnat in YOUUUURRRR shape?!?!!?!: ')
shape(int(numsides))

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
class Student:
    def __init__(self, email, name, grade):
        self.email = email
        self.name = name
        self.grade = grade

    def get_name(self):
        return self.name
    
    def get_email(self):
        return self.email
    
    def get_grade(self):
        return self.grade

    def set_name(self, new_name):
        self.name = new_name
    
    def set_email(self, new_email):
        self.email = new_email
    
    def set_grade(self, new_grade):
        self.grade = new_grade

    def __str__(self):
        return f"My name is {self.name}. My email is {self.email}. My grade is {self.grade}"

# Create an instance of the class corresponding to you
my_student = Student("yourmom@gmail.com", "Lincoln", "A+")

# Accessing attributes and printing the student information
print("Name:", my_student.get_name())
print("Email:", my_student.get_email())
print("Grade:", my_student.get_grade())

# Modifying attributes
my_student.set_name("Lincoln Crowell")
my_student.set_email("LBozo@hotmail.com")
my_student.set_grade("A")

# Printing updated information
print(my_student)

Name: Lincoln
Email: yourmom@gmail.com
Grade: A+
My name is Lincoln Crowell. My email is LBozo@hotmail.com. My grade is A