3.12-3.13 Procedures and Paramters Notes and Hacks
Markdown post of the group lesson for 3.12-3.13
Notes
Understanding Procedures
- A procedure is a set of instructions that is given a name which takes in parameters to return values
- also called methods and functions
- Sequencing, selection, and iteration are procedures
- Parameters are independent variables used in the procedure to produce a result
- Allows procredure to run without knowing the specific input values.
- As you an see, this procedure is named “multiply” its parameters are “x” and “y” and returns “The product of 5 times 3 is 15
Calling Procedures
- You call a procedure by putting the name of ot follwed by paranthesis with the parameters.
- Ex: def math(x)
Determining the result of a procedure
- Follow the code line by line to see what eaach line does
- Use syntax to determine
- function parameters and retunr value and statements.
-
to return the values you write “return” then the expression you want to return that has been defined.
- As you can see this code originally did not have return statement
- After debugging it with the return statement “return result” we can see the actual output of the function.
Managing Complexity
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
- Ex: assert “hypotenuse(3, 4) == hypotenuse_abstracted(3, 4) == 5”
- They would have the same result.
- They would have the same result.
- Ex: assert “hypotenuse(3, 4) == hypotenuse_abstracted(3, 4) == 5”
- 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
- having multiple duplicate code blocks, often decreasing readability and efficiency
- 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
Developing Procedures
- Parameters store variables and arguments
-
Call functions with procedure names (syntax in javascript and python)
- Procedure name: the name that was given to a function
- Arguments provied info to a functio and are usually defined outside of the function then imported into a function with parameters.
Python:
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
- defines the function with the parameters then prints their sum
- 1 + 2 is 3
- 2 + 3 is 5
function Function(a,b) {
return a + b;
}
Function(1,2)
Function(2,3)
3
5
- Calls the function with the parameters then adds them together
- 1 + 2 is 3
- 2 + 3 is 5
More Parameters
-
someetimes parameters will provide input to the function
-
This function provides 2 parameters and uses them as inputes and calling the function compares the 2 numbers.
Hacks
3.12
Procedure and Parameter
- Procedure: In an algorithm, it completes a certain task
- must be called to function
- Will take in parameters and will output values.
- Parameters: Variables that are usually used within procedures.
Quiz (3/3)
Return Values and Output Parameters
- Return Values: The result of the procedure once it is run
- Output Parameters: variables that are used within procedures that can lead to a certain output.
Code a square root procedure
3.13
Why is abstracting program logic into seperate modular functions effective?
- Abstracting a program into seperate modualr functions allows the coder to understand how each part of the code works. If it is a long, complex code it is better to break it down into a greater amount of more simpler modular functions so the program overall is simpler to read.
Procedure that uses other sub-procedures + explaination
- This program contains three sub-procedures: main(), print_foods(), and add_food().
- The main() procedure serves as the entry point for the program, and it calls the other two sub-procedures to perform specific tasks.
- The print_foods() procedure takes a list of foods as an argument and prints out each item in the list, while the add_food() procedure prompts the user for a new food to add to the list and then returns that new food.
- As you can see, I inputted “Falafel” as a food and it added falafel to the list of food.
Add another layer of abstraction to the word counter
Procedure Names and Arguments
- Procedure names: the names given to a method to produce an output
- Arguments: the data that is inputted into the function for example:
Code of Procedures, Arguments, and Parameters (interactive with HTML and JavaScript)
-
Here is an example of a calculator that uses arguments and parameters with Script and HTML.
- Write an expression using “+, -, /, or *” between TWO numbers and press calculate
- For example: 3*3 would output 9.
<!DOCTYPE html>