Sections 12-13
- Take some additional notes that you would like here for 3.12 and 3.13. We will be looking for additional notes from the presentation.
- What are procedures?
- Challenge 1 below: Add the command that will call the procedure.
- Challenge 2 below: Complete the Min and Max procedure in either JavaScript and Python using the instructions from the JavaScript page. (JavaScript will get you a extra 0.1)
- Homework/Hacks: For the hw, you have two options, easy or hard. The easy hack is for a 2.7 + extra work for the full 3. The easy hack is simply creating your own procedure with your own creativity. Since there is a lot of leeway for this one, you must do additional work to get a 3. For the hard hack, below is the start to a character to binary convertor. This is just a template, but the goal is to translate "APCSP" into binary. You can delete the existing code if you want. The only contraint is that you must use a procedure. Doing this will get you a 3.
- need to examine code line by line to determine what a procedure does
- proceude may or may not produce value
- proceudre needs to be defined and called
- proceudral abstracation: only know what is not done, not how
What are procedures?
Fill in the blanks please:
Procedure: named group of programming instructions that may have parameters and return values, can also be referred as method or function, depending on the language
Parameters: input values of a procedure
Arguments: specify the values of the parameters when a procedure is called
Modularity: change the actions if there is an error in the code
Procedural Abstraction: type of abstraction which provides a name for a process that allows a procedure to be used only knowing WHAT it does, not HOW it does it
What are some other names for procedures?: modularity 12222222 5
Why are procedures effective?: ability to alter the result without actually changing the calls to the program, convenient to change the actions if there is an error in the code (modularity)
x = 7
nums = []
def convertToBinary(x):
if x >= 1 and x <= 255:
while x/2 != 0:
rem = x % 2
nums.append(rem)
x = int(x/2)
nums.reverse()
binary = " "
for i in nums:
binary = binary + str(i)
return binary
else:
print("Number not within range.")
print(str(x) + " to binary: " + convertToBinary(x)) ## Function called here
function findMax(numA, numB) {
if (numA > numB) {
max = numA
min = numB
} else if (numA < numB) {
max = numB
min = numA
} else {
console.log("Numbers are equal.")
}
console.log("The max is " + max)
console.log("The min is " + min)
}
findMax(4, 5)
Homework/Hacks: For the hw, you have two options, easy or hard. The easy hack is for a 2.7 + extra work for the full 3. The easy hack is simply creating your own procedure with your own creativity. Since there is a lot of leeway for this one, you must do additional work to get a 3. For the hard hack, below is the start to a character to binary convertor. This is just a template, but the goal is to translate "APCSP" into binary. You can delete the existing code if you want. The only contraint is that you must use a procedure. Doing this will get you a 3.
def decToBin(x):
nums = []
while x/2 != 0:
rem = x % 2
nums.append(rem)
x = int(x/2)
nums.append(0)
nums.reverse()
binary = " "
for i in nums:
binary = binary + str(i)
print(binary)
return binary
def charToBin(x):
num_list = []
for i in x:
num_list.append(decToBin(ord(i)))
print(x + "to binary:" + str(num_list))
charToBin("APCSP")
# The output shown below is the output you are supposed to get