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.
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.
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
cost = 50
DISPLAY applyTax(cost, 10)
#It will display 55 (dollars)
# 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)
# Code here
def difference(num1, num2):
return abs(num1 - num2)
difference(-5, 3)
8
# 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);
class Car:
def __init__(self, attribues, vehicle_name, price):
self.attributes = attribues
self.vehicle_name = vehicle_name
self.price = price
mine = Car('Honda Civic', '2018', '13,000')
yours = Car('Toyota Prius', '2023', '28,000')
ours = Car('Chevrolet Impala', '2020', '22,000')
import 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):
angle = (180 / sides) * (sides - 2) # Gets the interior angle of the shape
for i in range(sides):
turtle.forward(50) #go forward 50 pixels or whatever size it is
turtle.right(angle) #Create an interior angle from the angle calculated before
numsides = input('How many sides do yoUUUU wnat in YOUUUURRRR shape?!?!!?!: ')
shape(int(numsides))
def get_distinct_values(arr):
distinct_values = []
for item in arr:
if item not in distinct_values:
distinct_values.append(item)
return distinct_values
# Test the function with the provided 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]
distinct_arr = get_distinct_values(arr1)
print(distinct_arr)
[2, 1, 3, 0, 4, 7, 5, 6]
class Student:
def __init__(self, email, name, grade):
self.email = email
self.name = name
self.grade = grade
def get_email(self):
return self.email
def get_name(self):
return self.name
def get_grade(self):
return self.grade
def set_email(self, email):
self.email = email
def set_name(self, name):
self.name = name
def set_grade(self, grade):
self.grade = grade
def to_string(self):
return f"My name is {self.name}, my email is {self.email}, and I am in {self.grade} grade."
your_student = Student("akhils25090@stu.powayusd.com", "Akhil Singamneni", "10th")
print(your_student.to_string())
My name is Akhil Singamneni, my email is akhils25090@stu.powayusd.com, and I am in 10th grade.