Unit 3 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.
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
- need to examine code line by line to determine this
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: provides a name for a process that allows a procedure to be used only knowing what it does, not how it does it
- allows a solution to a large problem based on the solutions of smaller subproblems
What are some other names for procedures?
- function
Why are procedures effective?
- they allow us to find out individually where something may have gone wrong
- you can look at only the function or place where something went wrong, rather than the whole
# def convertToBinary(n):
# (decimal)
# print()
# below is what I did
def DecimalToBinary(num):
strs = ""
while num:
# if (num & 1) = 1
if (num & 1):
strs += "1"
# if (num & 1) = 0
else:
strs += "0"
# right shift by 1
num >>= 1
return strs
# function to reverse the string
def reverse(strs):
print(strs[::-1])
# Code outputted
decimal = 8
print("Binary of 8 is: ", end=" ")
reverse(DecimalToBinary(decimal))
// Start by creating a procedure called findMax and set the parameters to numberA and numberB.
// Within the procedure, write the code to determine which of the two parameters, numberA or numberB, is the larger value. Print that value.
// Repeat the process, this time creating a procedure called findMin, which will print the parameter with a smaller value.
// Call both functions so that the parameters numberA and numberB are given a value.
// Optional bonus- create a procedure that can determine the minimum or maximum value out of more than two parameters.
var A =
fuction findMax {
}
A = int(input('Enter a number here:'))
B = int(input('Enter another number here:'))
# max function
def findMax ():
if A == B:
print(str(A) + " equals " + str(B))
elif A > B:
print(str(A) + " is the larger value")
elif B > A:
print(str(B) + " is the larger value")
findMax()
# min function
def findMin ():
if A == B:
print(str(A) + " equals " + str(B))
elif A > B:
print(str(B) + " is the smaller value")
elif B > A:
print(str(A) + " is the smaller value")
findMin()
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.
# first convert dec to binary
def convert(n):
binary = ""
while int(n) > 0:
binary+=str(int(n%2))
n = n / 2
return(binary[::-1]) # how to revese string
# next input string
letters = str(input('Enter a word in all CAPS'))
# ord makes letters into int
# now convert each letter
for p in letters:
c = convert(ord(p))
print (p + ": " + c)