Note Template
What are procedures?
Fill in the blanks please:
Procedure: A named group of programming instructions that may have parameters and return values. Procedures can also be referred to as a 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: Separating a program's functions into independent pieces or blocks, each containing all the parts needed to execute a single aspect of the functionality.
Procedural Abstraction: 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?: Depending on the language, procedures can also be referred to as a method or a function.
Why are procedures effective?: Procedures are effective because it gives us the ability to alter the result without actually changing the calls to the program. It also makes it convenient to change the actions if there is an error in the code.
Extra/Additional Notes
- A procedure may or may not return a value
- A procedure call interrupts a series of statements and makes the program execute the statements in the procedure
- Procedural abstraction allows a solution to a large problem based on the solution of smaller problems
- Some of the benefits of using procedures include...
- Gives the ability to alter the result without actually changing the calls to the program
- Allows us to break the code up and abstract what different parts of the code does (helps us identify/correct errors)
- Without procedures, we would have to look at all of the code line by line
decimal = 7
def convertToBinary(n):
b = ""
while n > 0:
r = n % 2
b = str(r) + b
n = n // 2
# this adds 0s to the left
return b.zfill(8)
print(convertToBinary(decimal))
In addition to the above code segment, I also created another segment in which the user can choose the number that they would like to convert into binary, which is shown below in Python:
def convertToBinary(n):
# b represents the binary number
b = ""
# n represents the decimal number that will be converted into the binary number
while n > 0:
# r represents the remainder
r = n % 2
b = str(r) + b
n = n // 2
return b.zfill(8)
n = int(input("What decimal number would you like to convert to binary?"))
print("User input: ", n)
b = convertToBinary(n)
print("The number {} in binary is {}".format(n,b))
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)
def findMax(numberA, numberB):
if numberA > numberB:
print(numberA)
else:
print(numberB)
def findMin(numberA, numberB):
if numberA < numberB:
print(numberA)
else:
print(numberB)
findMax(11, 43)
findMin(21, 910)
JavaScript Version
Note: Whenever I try running the JavaScript version, I always get that there is a syntax error. I have thoroughly looked through my code to make sure that I did not miss anything and have looked up to see if anyone else has had this problem. Unfortunately, I have had no luck getting my code to work. Below is what I would have done if my JavaScript kernel was working properly:
function findMax(numberA, numberB) {
if (numberA > numberB) {
console.log(numberA);
} else {
console.log(numberB);
}
}
function findMin(numberA, numberB) {
if (numberA < numberB) {
console.log(numberA);
} else {
console.log(numberB);
}
}
// test the functions
findMax(381, 8488320);
findMin(7, 1)
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 charToBinary(x):
binary_string = ""
for char in x:
binary_char = bin(ord(char))[2:] # this is what converts the character to binary and also removes the "0b" prefix so that it doesn't show up in the output
binary_string += binary_char + " "
return binary_string
# this allows the user to convert any set of strings into binary
x = input("Enter a string of characters:")
print("Enter a string of characters")
print("You entered {}".format(x))
# converts the string to binary and prints the result
binary_string = charToBinary(x)
print("{} in binary is {}".format(x, binary_string))