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?

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

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 and methods

Why are procedures effective?: Convenient to change errors, ability to alter the result without actually changing the calls to the program

Additional Notes

  • procedures are functions
  • procedures simplify programs
  • procedures split tasks into individual components that need to be done
  • procedures have parameters (what the procedure is going to work on/convert/etc)
  • seperating program to procedures in order to simplify it is called modularity

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

num = 7
def convert(num):
	
	if num >= 1:
		convert(num // 2)  # double slash : divide the first number by the second, rounds to the nearest integer
	print(num % 2, end = '') # end = '' signifies what will come between each printed num
 
# Driver Code
convert(num)
0111

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)

findMax = function(numberA, numberB){
    if (numberA>numberB) {
        max = numberA;
    } else {
        max = numberB;
    }
    console.log("The max is " + max);
};

findMin = function(numberA, numberB){
    if (numberA<numberB) {
        min = numberA;
    } else {
        min = numberB;
    }
    console.log("The min is " + min);
};

findMax(2,3)


findMin(6,10)
The max is 3
The min is 6

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 convert(num):
    if num >= 1:
        convert(num // 2)  # double slash : divide the first number by the second, rounds to the nearest integer
    print(num % 2, end = '') # end = '' signifies what will come between each printed num


def wordtobin(word):
    word = str(word)
    list = []
    for letter in word:
        list.append(letter)
    for item in list:
        index = list.index(item)
        new = ord(item)
        list[index] = new
    for i in list:
        z = list.index(i)
        new = convert(i)
        list[int(z)] = new

word = input("What word would you like to convert to binary?")
print(word, " to binary is:")
wordtobin(word)
APCSP  to binary is:
0100000101010000010000110101001101010000
word = "APCSP"
conversion = '-'.join(format(ord(x), 'b') for x in word)
print(word , "to binary is:" , conversion)
APCSP to binary is: 1000001-1010000-1000011-1010011-1010000

The Ord Function

  • built in (don't have to import it)
  • It takes one character in input (unicode) and outputs decimal number
  • inverse of chr function