Note Template
What are procedures?
Fill in the blanks please:
Procedure: Programming instructions, can have parameters/return values
Parameters: The input value in a procedure/function
Arguments: The values of the parameters when they are called.
Modularity: Separating functions into many pieces for better organization and to decompose a larger problem so that many parts of the code work to perform one function.
Procedural Abstraction: Gives a name (tells you what the task does) but does not show how a task is done. It takes in parameters and may execute different statements (selection) depending on the parameter. It is used to decompose a large problem by providing a solution to a smaller problem.
What are some other names for procedures?:
Other words for procedures are function/method.
Why are procedures effective?:
Procedures help split up the code into many parts. You can create functions that contain many lines of code, and then assign the function a specific name. This makes it easier when debugging because you can single out a specific function that is going wrong and debug them.
Additional Notes
3.12 Calling Procedures
Procedure call interrupts the code and executes the code inside the procedure. After that finishes, the program moves back to where it originally was when it called the procedure.
A procedure may return a value, but it does not need to do so
Calling a procedure: procedure name(arguments)
3.13 Developing Procedures
When making a procedure, make sure to do the following:
- Give the procedure a descriptive name
- Note what parameters are needed
- Note any required data
- Determine if the procedure returns a value or just simply does something
JavaScript notes
function name(parameters) {}
: Creates a function
decimal = 7
def convertToBinary(num):
numOfBinary = 8
binary = [];
output = ""
for i in range(numOfBinary):
binary.append((2**(numOfBinary - 1 - i)))
for i in range(len(binary)):
if num - binary[i] >= 0:
num -= binary[i]
output += "1"
else:
output += "0"
return(output)
conversion = convertToBinary(decimal)
print(conversion)
def findMax(numberA, numberB):
if numberA > numberB:
print("Maximum is: " + str(numberA))
else:
print("Maximum is: " + str(numberB))
def findMin(numberA, numberB):
if numberA < numberB:
print("Minimum is: " + str(numberA))
else:
print("Minimum is: " + str(numberB))
findMin(3, 5)
findMax(3, 5)
function findMax(numberA, numberB) {
if (numberA > numberB) {
console.log("Maximum is: " + numberA);
} else {
console.log("Maximum is: " + numberB);
}
}
function findMin(numberA, numberB) {
if (numberA < numberB) {
console.log("Minimum is: " + numberA);
} else {
console.log("Minimum is: " + numberB);
}
}
findMax(3, 5);
findMin(3, 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.
Comments
My code below uses Python. To convert the characters into ASCII, I used the ord()
function. I then created my own decimal to binary converter with the asciiToBinary()
function (procedure), using different code than the binary converter I made in Challenge 1.
While I was searching how to convert characters into ASCII, I also wondered if there was a Python function that convert decimal to binary. I found that the bin()
function could do the task, so below, I did the same homework using the bin()
function.
binaryList = []
def charToAscii(letter):
ascii = ord(letter)
asciiToBinary(ascii)
def asciiToBinary(ascii):
binaryNum = 0;
count = 0;
binaryString = "";
while binaryNum < ascii:
binaryNum = 2**count
count += 1
count -= 1
while (ascii > 0 and count >= 0):
if ascii >= 2**count:
ascii -= (2**count)
binaryString += "1"
count -= 1
else:
binaryString += "0"
count -= 1
count += 1
if count > 0:
while count > 0:
binaryString += "0"
count -= 1
binaryString = binaryString[1:len(binaryString)]
binaryList.append(binaryString)
charToAscii("A")
charToAscii("P")
charToAscii("C")
charToAscii("S")
charToAscii("P")
print("'APCSP' in binary is")
print(binaryList)
binaryList = []
def charToAscii(letter):
ascii = ord(letter)
asciiToBinary(ascii)
def asciiToBinary(ascii):
binaryString = bin(ascii)
binaryString = binaryString[2:len(binaryString)]
binaryList.append(binaryString)
charToAscii("A")
charToAscii("P")
charToAscii("C")
charToAscii("S")
charToAscii("P")
print("'APCSP' in binary is")
print(binaryList)
<p id="binary"></p>
<script>
const numOfBinary = 8;
let binary = [];
let ascii = "";
function charToAscii(letter) {
ascii = letter.charCodeAt(0);
asciiToBinary(ascii);
}
function asciiToBinary(ascii) {
binary.push(ascii.toString(2));
}
charToAscii("A");
charToAscii("P");
charToAscii("C");
charToAscii("S");
charToAscii("P");
document.getElementById("binary").innerHTML = "'APCSP' in binary is" + "<br>" + binary;
</script>