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)

Challenge 1 below: Add the command that will call the procedure.

decimal = 7

def convertToBinary(n):
    if n > 1:
        convertToBinary(n//2)
    print(n%2, end = "")

convertToBinary(decimal)
111

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)

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(5, 4);
let minimum1 = findMin(5, 4);
console.log("The maximum number of 5 and 4 is " + maximum1);
console.log("The minimum number of 5 and 4 is " + minimum1);
The maximum number of 5 and 4 is 5
The minimum number of 5 and 4 is 4

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):
    list = []
    newlist = []
    for i in x:
        letter = ord(i) - 64
        list.append(letter)
    for t in list:
        number = bin(t).replace("0b", "0")
        newlist.append(number)
    return newlist


text = "APCSP"
c = charToBinary(text)
print("Original text is: " + text)
print("The binary is: ", c)
Original text is: APCSP
The binary is:  ['01', '010000', '011', '010011', '010000']