Essential Knowledge

Procedure: also called a function, it is a small section or piece of code which does something specific or does a specific task

Parameter: variable passed into a function

a=1
b=9
# parameters are a and b
def addition(a, b):
    sum = a + b
    return sum
# call function print
print(addition(a, b))
10

Return Values: function that returns to called function when the task is completed

Output Parameters: refers to one section of the code as an input to function

Procedure Names: ex. in this code the procedure's name is 9 and 10

x = 9
y = 10

Arguments: a way to give more information to functions

  1. Why abstracting away your program logic into separate, modular functions is effective

It makes the code easier to understand, maintainable, and organized. Breaking the code down makes it easier to debug and organize the code


Quiz

Complete quiz:

questionNum = 3
correct = 0
questions = [
    "What is are correct names for a procedure? \n A) Method \n B) Function \n C) Both",
    "What is a procedure? \n A) Sequencing \n B) Selection \n C) Iteration \n D) All",
    "Use this for following question: \n def inchesToFeet(lengthInches): \n\t lengthFeet = lengthInches / 12 \n\t return lengthFeet \n\n What is the procedure name, the parameter, and what the procedure returns? \n A) feetToInches, lengthInches, lengthMeters \n B) inchesToFeet, lengthInches, lengthFeet \n C) inchesToFeet, lengthFeet, lengthInches \n D) lengthInches, inchesToFeet, lengthFeet"]
answers = ["c", "d", "b"]

def qna(question, answer):
    print("Question:", question)
    response = input()
    print("Answer:", response)
    
    if response.lower() == answer:
        print("Correct :) \n")
        global correct
        correct += 1
    else:
        print("Incorrect :( \n")
for x in range(questionNum):
    qna(questions[x], answers[x])
    
print("Score:", correct, "/ 3")
Question: What is are correct names for a procedure? 
 A) Method 
 B) Function 
 C) Both
Answer: c
Correct :) 

Question: What is a procedure? 
 A) Sequencing 
 B) Selection 
 C) Iteration 
 D) All
Answer: d
Correct :) 

Question: Use this for following question: 
 def inchesToFeet(lengthInches): 
	 lengthFeet = lengthInches / 12 
	 return lengthFeet 

 What is the procedure name, the parameter, and what the procedure returns? 
 A) feetToInches, lengthInches, lengthMeters 
 B) inchesToFeet, lengthInches, lengthFeet 
 C) inchesToFeet, lengthFeet, lengthInches 
 D) lengthInches, inchesToFeet, lengthFeet
Answer: b
Correct :) 

Score: 3 / 3

Procedure Code

Code a procedure that finds the square root of any given number. (make sure to call and return the function)

# import
import math

a = 25
# a is the parameter
def sqrt(a):
    return math.sqrt(a)
# calling the func
print(sqrt(a))
5.0
# import
import math

user = float(input())

sqrt = math.sqrt(user)
print("Type a number:" , user)
print(sqrt)
Type a number: 25.0
5.0

Sub-Procedures

  1. Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, ect.)
  2. Add another layer of abstraction to the word counter program (HINT: create a function that can count the number of words starting with ANY character in a given string -- how can we leverage parameters for this?)
a = 5
b = 10

# func
def add(a,b):
    sum = a + b
    return(sum)

# result
print(add(a,b))
15
def split_string(s):
    # listing words and splitting string
    words = s.split(" ")

    new_words = []
    for word in words:
        if word != "":
            # `words` to `new_words`
            new_words.append(word)
    
    return words

# input and returns the number of words
# given letter
def count_words_starting_with_letter(words, letter):
    count = 0
    
    # loop through the list
    for word in words:
        # use lower() 
        if word.lower().startswith(letter):
            count += 1
    
    return count

# returns the number of words that start with 'a'
def count_words_starting_with_a_in_string(s):
    # use the split_string() function to split the input string into a list of words
    words = split_string(s)
    
    # use count_words_starting_with_letter() 
    count = count_words_starting_with_letter(words, "a")
    
    return count

def count_words_starting_with_d_in_string(s):
    words = split_string(s)
    count = count_words_starting_with_letter(words, "d")
    return count

def any_count(sentence, letter):
    
    words = split_string(sentence)

    count = count_words_starting_with_letter(words, letter)

    return count

userLetter = input("Input any letter:")
answer = count_words_starting_with_letter(userLetter, str(userLetter))
print(str(answer) + " word(s) starting with " + str(userLetter))
1 word(s) starting with a

Procedures, Arguments, Parameters

Code some procedures that use arguments and parameters with Javascript and HTML (make sure they are interactive on your hacks page, allowing the user to input numbers and lick a button to produce an output)

  • add two numbers
  • subtract two numbers
  • multiply two numbers
  • divide two numbers

x = 14 y = 19