Unit 3.12 - 3.13 Procedures and Parameters
Lesson 3.12 & 3.13 Notes
3.12 Notes
- A procedure is a named set of instructions that can take in parameters and after the code is run, return values.
- Parameters are used inside of procedures as independent variables. Parameters can be used in 3 ways, those being sequences, selections, and iterations.
- You can call a procedure by writing the name of a procedure and adding the parameters in parentheses.
- To return values you must write return followed by the expression.
- Arguments are what is printed
- Return is necessary for functions to work. ## 3.13 Notes
- Modularity: Breaking complex programs into easier parts
- Abstraction: Hides parts and exposes what is necessary
- Duplication: Multiple codes of the same thing, reduces efficacy as it can be simplified.
- Steps that a computer follows
- Procedure name is the name given to a function or procedure.
- Arguments provide information to a function, kind of like a what to do.
- Collegeboard uses similar wording but some are shortened.
Hacks
3.12 Hacks
- Define procedure and parameter in your own words
- A procedure does a specific part and must be called, such as multiplying two numbers together The procedure will take in parameters and will output values. A parameter is just variables, typically found within procedures.
- Paste a screenshot of completion of the 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")
- Define Return Values and Output Parameters in your own words Return values are are what is resulted once a procedure is run. Parameters are variables that are used within procedures that can lead to the output.
- Code a procedure that finds the square root of any given number. (make sure to call and return the function)
x=27
import math
def squarert(x):
squareroot = x ** 0.5
return squareroot
squareroot = squarert(x)
print("The square root of ", x, "is ", squareroot)
Topic 3.13 (3.B):
- Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
- Abstracting a program allows the coder to understand how the code works. This helps for long pieces of code so that the coder would be able to know what each function accomplishes and it allows them to better troubleshoot any possible issues.
- Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.) This is an example of a procedure having sub procedures, in this case, there are two different smaller procedures based on the size of x. This makes it easier for the coder to see their work as well as it can shorten the length of code.
def big_function():
x=84
if x > 23:
def big_num():
print("that is a big number")
big_num()
else:
def small_num():
print("That is a small number")
small_num()
big_function()
def split_string(s):
words = s.split(" ")
new_words = []
for word in words:
if word != "":
new_words.append(word)
return words
def count_words_starting_with_letter(words, letter):
count = 0
for word in words:
if word.lower().startswith(letter):
count += 1
return count
def count_words_starting_with_n_in_string(s):
words = split_string(s)
count = count_words_starting_with_letter(words, "n")
return count
def count_words_starting_with_c_in_string(s):
words = split_string(s)
count = count_words_starting_with_letter(words, "c")
return count
# example usage:
s = "I guess that I am tryna find the words in this sentence start with n or c, you know, cus thats my initials, nathan capule, yeah, its kinda obvious "
n_count = count_words_starting_with_n_in_string(s)
c_count = count_words_starting_with_c_in_string(s)
print("Words starting with n:", n_count)
print("Words starting with c:", c_count)
Topic 3.13 (3.C):
- Define procedure names and arguments in your own words. Procedure names are just the names given to a function while arguments are the values given to a called function.
- 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
var a = 13
var b = 12
var c = 6