Notes

  • A procedure is a named set of instructions that can take in parameters and return values
    • May be called "method" or "function" in different programming languages
      • Parameters are independent variables used in the procedure to produce a result
    • Allows procedure to execute without initially knowing specific input values
    • used to make functions work with multiple different inputs
      • To call a procedure you write the name of the procedure followed by the parentheses with the parameters
    • procedureName(parameter1, parameter2, ...)
    • Procedures do not require parameters, but parentheses is required
      • Using syntax, you can determine the result with function parameters and return value and statements
    • To use function parameters, paremeters used when calling the function in order to get result,you would have to write the syntax name of function followed by parameters in parentheses
      • A return statement exits a function and instructs python to continue executing the program and return a certain value
    • Value can be string, tuple, etc. being sent back to the main program
      • Modularity: the practive of breaking a complex program into smaller, independent parts that can be used/reused in different parts of the program
      • Abstraction: the practice of hiding the details of how a code/system works, only exposing essential functions necessary for other parts of the program
      • Duplication: Having multiple duplicate code blocks decreasing efficiancy
      • Logic: the sequence of steps that a computer follows to execute a program
      • Procedure Name: name that is given to a procedure, to call it
      • Arguments: way to provide info to a function, defined outside a function then imported into a function with parameters
      • in python, def defines function

Hacks

Topic 3.12 (3.A)

  1. Define procedure and parameter in your own words

procedure: a section of code that takes parameters and returns values
parameter: a variable used in a function to produce a result

  1. Paste a screenshot of completion of the quiz

Quiz Score

  1. Define Return Values and Output Parameters in your own words

return values: the result of a function returned to the caller
output parameters: parameters used for output, passed into a function or method call that are modified during the function

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

num = int(input("Find the square root of:"))

def sqrt(num):
    result = math.sqrt(num)
    return result
    
print(sqrt(num))
9.0

Topic 3.13 (3.B)

  1. Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective

Abstraction makes your code easier to manage and understand. It allows you to focus on the real purpose of the function.

  1. Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
import math

print("Find the hypotenuse of a right triangle!")

a = int(input("Input side a"))
print("Side a =", a)
b = int(input("Input side b"))
print("Side b =", b)

def side(x):
    return x * x
def equation(a, b):
    a = side(a)
    b = side(b)
    result = math.sqrt(a + b)
    return result

print("Hypotenuse is", equation(a, b))
Find the hypotenuse of a right triangle!
Side a = 3
Side b = 4
Hypotenuse is 5.0

Both side a and b were to be squared, so by creating the side() function, I do not have to repeat that step, instead assigning it to each, producing a result instantly. I could call this function in my next function to be efficient.

  1. 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?)
x = input("Input letter!")

# this function takes a string as input and returns a list of words, where each word
# is a separate element in the list
def split_string(s):
    # use the split() method to split the string into a list of words
    words = s.split(" ")

	# initialize a new list to hold all non-empty strings
    new_words = []
    for word in words:
        if word != "":
            # add all non-empty substrings of `words` to `new_words`
            new_words.append(word)
    
    return words

# this function takes a list of words as input and returns the number of words
# that start with the given letter (case-insensitive)
def count_words_starting_with_letter(words, letter):
    count = 0
    
    # loop through the list of words and check if each word starts with the given letter
    for word in words:
        # use the lower() method to make the comparison case-insensitive
        if word.lower().startswith(letter):
            count += 1
    
    return count

# this function takes a string as input and returns the number of words that start with 'a'
def count_words_starting_with_string(s):
    # use the split_string() function to split the input string into a list of words
    words = split_string(s)
    
    # use the count_words_starting_with_letter() function to count the number of words
    # that start with 'a' in the list of words
    count = count_words_starting_with_letter(words, x)
    return count

# example usage:
s = "Sandy sells seashells by the sea shore."
letter_count = count_words_starting_with_string(s)
print("Words starting with", x, ":", letter_count)
Words starting with s : 5

Topic 3.13 (3.C)

  1. Define procedure names and arguments in your own words.

procedure names: the name of a function or procedure that can called anywhere in the program
arguments: provides information to a function, imported into a function with parameters (defined outside function)

  1. 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 click a button to produce an output)
    • Add two numbers
    • Subtract two numbers
    • Multiply two numbers
    • Divide two numbers
<html>
<body>
<form>
1st Number: <input type="text" id="firstNumber" /><br>
2nd Number: <input type="text" id="secondNumber" /><br>
<input type="button" onClick="add(num1, num2)" Value="+" />
<input type="button" onClick="subtract(num1, num2)" Value="-" />
<input type="button" onClick="multiply(num1, num2)" Value="*" />
<input type="button" onClick="divide(num1, num2)" Value="/" />
</form>
<p>Result: <br>
<span id = "result"></span>
</p>
</body>
<script>
  function add(num1, num2)
{
        num1 = parseInt(document.getElementById("firstNumber").value);
        num2 = parseInt(document.getElementById("secondNumber").value);
        sum = num1 + num2;
        document.getElementById("result").innerHTML = sum;
}
  function subtract(num1, num2)
{
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
        document.getElementById("result").innerHTML = num1 - num2;
}
  function multiply(num1, num2)
{
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
        document.getElementById("result").innerHTML = num1 * num2;
}
  function divide(num1, num2) 
{ 
        num1 = document.getElementById("firstNumber").value;
        num2 = document.getElementById("secondNumber").value;
        document.getElementById("result").innerHTML = num1 / num2;
}
</script>
</html>
1st Number :
2nd Number:

Result: