- 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?
#code here
def applyTax(cost, tax):
    taxed = cost * tax/100
    finalCost = cost + taxed
    return finalCost
applyTax(50,10)
55.0
What Are Functions?
- Collections of code
- Divides large program into smaller chunks
- Better readability
- Less repetitive code
- More efficient code
- Good organization
What Are The Components of a Function?
- The function declaration
- The parameters (input). This is also referred to as an argument when a value is being passed to the actual function.
- The functionality
- The return value (output)
- Calling the 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
num1 = int(input("Enter a number"))
num2 = int(input("Enter a number to subtract from the original"))
def subtract(x, y):
    return x - y
subtract(num1, num2)
3453456
What is a Class?
- A class is an outline for a set of nested functions and variables.
- There are instance variables
- Functions
- Constructor method (Required)
- To String method
- Getter method
- Setter method
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);
My name is John Doe and I am 15 years old.
My name is John Doe and I am 15 years old.
Popcorn Hack 3:
1. Create a Car class which has the attributes model, vehicle name, and price
2. Create instances of the following cars
- Name: Honda Civic , Model Year: 2018 , Price: $13,000
- Name: Toyota Prius, Model Year: 2023 , Price: $28,000
- Name: Chevrolet Impala, Model Year: 2020 , Price: $22,000
class car:
    def __init__(self, model, name, price,):
        self.model = model
        self.name = name
        self.price = price
    
car1 = car("Honda Civic", 2018, 13000)
car2 = car("Toyota Prius", 2023, 28000)
car3 = car("Chevrolet Impala", 2020, 22000)
Homework:
Assignment 0: How do you use functions?
Create a turtle python function that...
- Takes a single parameter as the number of sides
- Outputs a shape corresponding to the number of sides
- Call the function with the argument being a variable with the user input
Hint:
# Turtle does not work on school wifi. Copy paste into a replit if you wanna see if I did it right
import turtle
pen = turtle.Turtle()
angle = 0
sides = int(input("How many sides? "))
if sides == 1 or sides == 2:
    print("Please enter a SHAPE")
else:
    angle = 360/sides
    
for i in range(sides):
    pen.forward(30)
    pen.right(angle)
    i += 1
turtle.done()
Assignment 1
Create a function that...
- Takes an array as the parameter
- Returns the array of distinct values
- Don't use test arrays
arr1 = []
arr2 = []
while True:
    add = input("Add a number. Enter 's' to stop")
    if add == "s":
        break
    arr1.append(int(add))
    if int(add) not in arr2:
        arr2.append(int(add))
print("Numbers inputed: ",arr1)
print("After duplicates removed: ",arr2)
Numbers inputed:  [2, 23, 34, 2, 2, 6, 45, 2, 34, 45, 21, 2, 2, 9]
After duplicates removed:  [2, 23, 34, 6, 45, 21, 9]
Assignment 2:
Create a student class that...
- Has a constructor that takes three parameters as attributes
- 
        - name
- grade
 
- Three getter methods to access the name, email, and grade
- Three setter methods to modify the name, email, and grade
- 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}
- Create an instance of the class that corresponds with you
class person:
    def __init__(self, email, name, grade, ): # constructor
        self.email = email
        self.name = name
        self.grade = grade
    
    def getEmail(self): # method to create get email
        return self.email
    
    def getName(self): # method to create get name
        return self.name
    
    def getGrade(self): # method to create get grade
        return self.grade
    
    def setEmail(self, email): # method to create set email
        self.email = email
    
    def setName(self, name): # method to create set name
        self.name = name
        
    def setGrade(self, grade): # method to create set grade
        self.grade = grade
        
    def __str__(self): # method that returns a string when the object is printed
        return (f"My name is {self.name} and I am in {self.grade}th grade. My email is {self.email}")
trevor_huang = person('fake@gmail.com', 'Trevor Huang', 10)
print(trevor_huang)
My name is Trevor Huang and I am in 10th grade. My email is fake@gmail.com