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))
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
- 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
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")
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))
# import
import math
user = float(input())
sqrt = math.sqrt(user)
print("Type a number:" , user)
print(sqrt)
- Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, ect.)
- 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))
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))
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