Lesson 3.12-3.13 hacks
Hacks
Units 3.12 and 3.13 Notes
Essential Knowledge:
- 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. It allows a procedure to execute without initially knowing specific input values.
- Procedures can be classified as sequencing, selection, and iteration. How?
Example:
- What is the procedure's name?
- What are the parameters?
- What did the procedure return?
-
To determine the result of a procedure or any code, you must follow the code line by line and see what each one does
-
Using syntax, you can determine the result by
- function parameters
- return value and statements
-
A return statement exits a function and instructs python to continue executing the program and to return a certain value
-
Value can be string, a tuple, or any other type that is being sent back to the main program
- Modularity - the practice of breaking a complex program into smaller, independent parts or modules that can be used and reused in different parts of the program
- Abstraction - the practice of hiding the details of how a particular code or system works and exposing only the essential features or functions that are necessary for other parts of the program to use
- Duplication - having multiple duplicate code blocks, often decreasing readability and efficiency
- Logic - the sequence of steps and operations that a computer follows to execute a program, including the specific instructions and decision-making processes built into the code.
- Procedure - a module of code that is created to complete a certain task, this is basically a function
- Procedure Name - the name that is given to a function/procedure
- Parameters - a variable that is used in a function to allow for data to be imported into a function
-
Arguments - a way to provide information to a function, usually defined outside a function and then imported into a function with parameters
-
parameters being used to manage complexity
- parameters storing variables
- parameters storing arguments
- calling functions with procedure names
- choosing procedure names
- calling procedures in python and javascript
def function(a,b): # function is defined
print(a+b) # prints output of variables
function(1,2) # one instance that it can be used
function(2,3) # another instance
``` 3
5
Hacks 3.12 3.13
- Define procedure and parameter in your own words Procedures or methods are basically the instructions of a function but are assigned to a variable. Unlike assigning a singular item to a class, you assign a set of instructions. Parameters are the part of a function that include data which allow the function to work.
Here is an example of both being put into use
num1=2
num2=3
def addition(num1, num2): #the procedure is addition, the parameters are num1 and num2
sum = num1 + num2
return sum
print(addition(num1, num2)) #here we are calling the function addition and printing it
5 #this is the output
- Paste a screenshot of completion of the quiz

- Define Return Values and Output Parameters in your own words Return values are the output of the procedure... It basically "returns" what the procedure does. Output Parameters are essentially the same as regular parameters
def addition(num1, num2): #the procedure is addition, the parameters are num1 and num2
sum = num1 + num2
return sum #this is the return function
5 #this is the return output
- Code a procedure that finds the square root of any given number. (make sure to call and return the function)
import math
x = 9 #parameter
def sqrt(x):
return math.sqrt(x)
print(sqrt(x)) #calling the function and printing
Topic 3.13 (3.B):
- Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
Abstracting away your program logic into separate, modular functions is effective because it helps to reduce code duplication and it is easier to manage in general.
- Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
x = 4
y = 3
#find the solution to a simple arithmetic equation: 5(x+y)
def mathematics(x, y):
sum = x + y
return sum/5
print(mathematics(x, y))
Abstraction was necessary because it helped to reduce the clutter of the function. If I were to take sum and make another function dividing sum by 5, I would need make a block of code and I wouldn't be able to call it again when I needed to.
- 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?)
Topic 3.13 (3.C):
- Define procedure names and arguments in your own words.
- 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 -idk
# 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_a_in_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, "a")
return count
# see above
def count_words_starting_with_d_in_string(s):
words = split_string(s)
count = count_words_starting_with_letter(words, "d")
return count
# example usage:
s = " This is a test string! Don't you think this is cool? "
a_count = count_words_starting_with_a_in_string(s)
d_count = count_words_starting_with_d_in_string(s)
print("Words starting with a:", a_count)
print("Words starting with d:", d_count)
- Define procedure names and arguments in your own words.
Procedure names are the names given to a method to produce an output for example:
Arguments are basically the data that is inputted into the function for example:
x = 1 # x and y are parameters that will be inputted into the procedure.
y = 2
def matematicas(x, y): #matematicas would be the procedure name.
addition = x + y
return addition
print(matematicas(x,y))
3.0
- 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
x = 1 y = 2
Extra credit
import math
def hypotenuse(leg1, leg2):
# notice we're using this <var> * <var> syntax multiple times?
# this has multiple drawbacks:
# - it's repetitive and makes the code longer
# - if we wanted to change the operator being
# applied to `leg1` and `leg2`, we'd have to do it twice!
leg1_squared = leg1 * leg1
leg2_squared = leg2 * leg2
return math.sqrt(leg1_squared + leg2_squared)
## VERSUS ##
# this works, but let's try to write the "squared" variable assignment statements more concisely....
def square(a):
return a * a
def hypotenuse_abstracted(leg1, leg2):
# not only is this shorter, but we can now:
# - better understand the code at a glance--we know exactly
# what `square` should do
# - change the operator in a single place (`square`) rather than
# multiple times within this hypotenuse function
leg1_squared = square(leg1)
leg2_squared = square(leg2)
return math.sqrt(leg1_squared + leg2_squared)
## EXTRA CHALLENGE ##
# is it possible to write the `hypotenuse` function in a single line?
def hypotenuse_abstracted2(leg1, leg2):
return math.sqrt(square(leg1)+square(leg2))
print(hypotenuse_abstracted2(3,4))
assert hypotenuse(3, 4) == hypotenuse_abstracted(3, 4) == 5