Note Template
What are procedures?
Fill in the blanks please:
Procedure: a named group of programming instructions that may have parameters and return values
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: allows a procedure to be used only knowing WHAT it does, not HOW it does it
What are some other names for procedures?: methods or functions, it all depends on the language
Why are procedures effective?: allows for the changing of code without altering the program calls, making it much easier and more efficient than reading line-by-line
Additional Notes
- Procedure calls interrupt a series of statements so the program executes the statements in the procedure
- After it is complete, it returns to the original statements to finish them
- It is necessary to examine the code line by line to confirm a procedure
- A procedure MAY or MAY NOT return a number or boolean
- To call a procedure you would write the name of the procedure followed by the parentheses with the parameters of the procedure
Questions to ask when developing a procedure
- What am I going to name my procedure? (It should reflect the purpose of the code)
- What parameters do I need?
- What data would I need to take in to accomplish my goal?
- Do I want my procedure to give a numerical value, or complete an action?
decimal = 7
def convertToBinary(n):
binary = ""
while int(n) > 0:
binary+=str(int(n%2))
n = n / 2
print(binary[::-1]) # how to reverse string
n = int(input('Enter the decimal integer of choice'))
convertToBinary(n) # I entered 7
// my javascript kernel is broken and has never worked, but I did the challenge in js anyways, it just won't run
function findmax(numA,numB) {
if (numA > numB) {
return numA;
}
else {
return numB;
}
}
function findmin(numA, numB) {
if (numA < numB) {
return numA;
}
else {
return numB
}
}
console.log(findmax(1, 10));
console.log(findmin(1, 10));
Homework/Hacks: For the hw, you have two options. The first hack is for a 0.7 + extra work for the full 1. Simply create your own procedure with your own creativity. Since there is a lot of leeway for this one, you must do a LOT additional work to get a 1. For the other option, below is the start to a character to binary convertor. This is just a template, but the goal is to translate "APCSP" into binary. The only contraint is that you must use a procedure. If this is completed along with extra work, you can get a 1.
def convertToBinary(n):
if n >= 1:
convertToBinary(n // 2)
print(n % 2, end = '')
def bin(word):
list = []
for character in str(word):
list.append(character)
for item in list:
index = list.index(item)
unicode = ord(item) # ord function returns the unicode for a character
list[index] = unicode
for i in list:
x = list.index(i)
new = convertToBinary(i)
list[int(x)] = unicode
word = input("enter word")
print(word, " converted to binary is ")
bin(word)