3.12 and 3.13
Notes
Procedure: A 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: The compartmentalization and interrelation of the parts of code
Procedural Abstraction: Provides a name for a process that allows a procedure to be used only knowing what it does
- Have variable parameters
- Code handles different situations depending on how its parameters are set/called
- It allows a solution to a large problem based on the solutions of smaller subproblems. What are some other names for procedures?: Another name for procedures are function. Procedure names can be any name we want, i.e. decimalToBinary()
Why are procedures effective?: Have the 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
- Able to break the code up and abstract what different part of the code does; helps identiy bugs, error, etc.
- Much better than reviewing code without a procedure (you would have to look at every line by line)
What are procedures?
Fill in the blanks please:
Procedure: is a named group of programming instructions that may have parameters and return values.
Parameters: are 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.
What are some other names for procedures?: Functions
Why are procedures effective?: We have the ability to alter the result without actually changing the calls to the program
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])
# Driver Code
num = 7
print("Binary of num 7 is:", end=" ")
reverse(DecimalToBinary(num))
function findMax(numberA, numberB){
if (numberA > numberB){
return numberA;
} else {
return numberB;
}
}
function FindMin(numberA,numberB){
if (numberA < numberB){
return numberA;
} else {
return numberB;
}
}
let maximum1 = findMax(10, 20);
let minimum1 = FindMin(10, 20);
console.log("The maximum number of 10 and 20 is " + maximum1);
console.log("The minimum number of 10 and 20 is " + minimum1);
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 charToBinary(x):
binary = []
for char in x:
letter = ord(char) - 64
binary.append(bin(letter).replace("0b", "0"))
return binary
text = "APCSP"
c = charToBinary(text)
print("Original text is: " + text)
print("The binary is: ", c)