Unit 3 Sections 12 and 13 notes
Overview: Learning Objectives
- Basics of Procedures
- Calling Procedures
- Determine Results of Procedures
What is a procedure?
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?
How do you call a procedure?
- write the name of the procedure and parenthesis with the parameters of the procedure.
example
num = 5
def math(x):
op1 = x * 2
op2 = op1 - 9
return op2
Hacks
Topic 3.12 (3.A):
- Define procedure and parameter in your own words
- A procedure is a part of the code that completes a certain part.
- A parameter is a variable that gets used in another function to show the data of a variable.
-
Paste a screenshot of completion of the quiz
-
Define Return Values and Output Parameters in your own words
- A return value is a function that recalls a variable that has been defined previously. An output parameter is when make limits on what the output can be.
- Code a procedure that finds the square root of any given number. (make sure to call and return the function)
import math
x = 4
y = 8
def square_root(x):
square_root = x
return square_root
answer = math.sqrt(x)
print(answer)
Topic 3.13 (3.B):
- Explain, in your own words, why abstracting away your program logic into separate, modular functions is effective
- Abstracting your code makes it easier to organize and reuse your code.
- Create a procedure that uses other sub-procedures (other functions) within it and explain why the abstraction was needed (conciseness, shared behavior, etc.)
- 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?)
makes it easier to organize the code.
- Procedure names: a procedure names is something that calls the procedure, used to make it easier later on in the code. An argument, is what allows the procedure to do whatever it needs with the arguments provided.
import math
def calculate_average(numbers: [1, 2, 3, 4]):
total = 0
for num in numbers:
total += num
return total / len(numbers)