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?
def applyTax(cost, percentTaxed):
temp = 100 + percentTaxed
temp = temp/ 100
cost = cost * temp
return cost
tax = applyTax(50, 10)
print(tax)
55.00000000000001
# 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)
10
def subtract(number1, number2):
answer = number1 - number2;
return answer
print(subtract(6,4))
2
# 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.
class Car:
def __init__(self, model, vehicle, price):
self.model = model;
self.vehicle = vehicle;
self.price = price;
print("The car model is " + " " + self.model + " " + "The vehicle is a " + self.vehicle + " " "The price is a " + " "+ self.price)
CarAttribute = Car("2018", "Honda Civic", "$13,000")
CarAttribute2 = Car("2023", "Toyota Prius", "$28,000")
CarAttribute3 = Car("2020", "Chevrolet Impala", "$22,000")
print(CarAttribute)
print(CarAttribute2)
print(CarAttribute3)
The car model is 2018 The vehicle is a Honda Civic The price is a $13,000
The car model is 2023 The vehicle is a Toyota Prius The price is a $28,000
The car model is 2020 The vehicle is a Chevrolet Impala The price is a $22,000
<__main__.Car object at 0x7f36c47c3c40>
<__main__.Car object at 0x7f36c47c0df0>
<__main__.Car object at 0x7f36c47c29b0>
import turtle
pen = turtle.Turtle()
def shape(sides):
angle = 360 / sides
for x in range(sides):
pen.forward(50)
pen.right(angle)
numsides = input('How many sides do yoUUUU want in YOUUUURRRR shape?!?!!?!: ')
shape(str(numsides))
class personalInformation:
def __init__(self, name, email, grade):
self.name = name;
self.email = email;
self.grade = grade;
def getName(self):
return self.name
def getEmail(self):
return self.email
def getGrade(self):
return self.grade
def setName(self):
self.name = name;
def setEmail(self):
self.email = email;
def setGrade(self):
self.grade = grade;
def __str__(self):
return (f"My name is {self.name}, My email is {self.email} and I am in {self.grade} grade.")
information = personalInformation("Nandan", "exampleemail@gmail.com", "11th")
print(information)
My name is Nandan, My email is exampleemail@gmail.com and I am in 11th grade.