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?

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 is called

Procedural Abstraction: which 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?: function

Why are procedures effective?: We 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 (modularity). 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)

Extra Notes

  • A procedure call interrupts a series of statements and makes the program execute the statements in the procedure. After executing, it will return to the original call and finish the statements.
  • in python, to call a procedure you would write the name of the procedure followed by the parentheses with the parameters of the procedure (there may not be parameters, but parenthesis must be there).
  • procedural abstraction: process that allows a procedure to be used only knowing WHAT it does, not HOW it does it.
    • 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.
    • Separating a program's functions into independent pieces or blocks, each containing all the parts needed to execute a single aspect of the functionality is called modularity
function subtraction(a,b) {
    return a - b;
  }
  
  subtraction(10,12)

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

def dectobinary(decimal):
    binary = 0
    i = 0
    num = decimal
    while(num > 0):
        binary = ((num%2)*(10**i)) + binary # set binary equal to remainder of the input/2, multiplied by 10^i
        num = int(num/2) #now number equal num/2 rounded to an integer
        i += 1 #incriment by 1
    print("Your number " + str(decimal) + " in binary is " + str(binary))  #output result       

decimal = 7
dectobinary(decimal)
Your number 7 in binary is 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)

a = int(input("Enter your first value"))
b= int(input("Enter your second value?"))

def function(a,b):
    if a > b:
        print(str(a) + " (a) is the larger value!")
        print(str(b) + " (b) is the smaller value!")
    else:
        if b > a:
            print(str(b) + " (b) is the larger value")
            print(str(a) + " (a) is the smaller value!")
        if b == a:
            print(str(a) + str(b) + " (a and b) are equal")

function(a,b)
4 (b) is the larger value
1 (a) is the smaller value!
a = int(input("Enter your first value"))
b= int(input("Enter your second value?"))

def findMax(a,b):
    if a > b:
        print(str(a) + " (a) is the larger value!")
    else :
        if b > a:
            print(str(b) + " (b) is the larger value!")

def findMin(a,b):
    if a < b:
        print(str(a) + " (a) is the smaller value!")
    else:
        if b < a:
            print(str(b) + " (b) is the smaller value!")

findMax(a,b)
findMin(a,b)
4 (b) is the larger value!
1 (a) is the smaller value!

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.

import math

def charToBinary(term):
  char = []
  binary = [] 
  for x in term:
    char.append(ord(x)) #each character from APCSP is assigned to a list (char)
  for x in char:
    binary.append(int(bin(x)[2:])) #for each character in list char, the binary of that character is appended to binary list (I just used the built in bin conversion function)
  return binary #binary list is printed

print(" 'APCSP' in binary is: ") 
print(charToBinary("APCSP")) 

# The output shown below is the output you are supposed to get
 'APCSP' in binary is: 
[1000001, 1010000, 1000011, 1010011, 1010000]