Lesson Plan
3.12: Calling Procedures
- A Procedure is a named group of programming instructions that may have parameters and return values.
- Procedures can also be referred as method or function, depending on the language
- Parameters are input values of a procedure.
- Arguments specify the values of the parameters when a procedure is called
A procedure call interrupts a series of statements and makes the program execute the statements in the procedure. After executing, it will return to the original call and finish the statements.
In order to determine what the result of a procedure is, you need to carefully examine the code line by line
A procedure MAY or MAY NOT return a value (a number or boolean (true or false)).
- Before we start writing the procedure, we need to know what arguments the procedures require and and what type of data the procedure will return.
- We need to be able to call the procedure.
- To call a procedure you would write the name of the procedure followed by the parentheses with the parameters of the procedure (there may not be parameters, but parenthesis must be there).
Here is an example
def square(x): # def is used to define a procedure; in this case, it is called square, which is a math function that squares input values. The paramter is x inside of the parenthesis.
return x*x # x is multiplied by x
print(square(6)) # Here, the function is called with the argument "6" which essentially replaces x for 6.
Lets look at more examples:
Here is another square function, this time, with different names, parameters, and arguments. Can you spot them?
def multiplyNum(num): # procedure is defined with the parameter num
return num * 8 # num will be multipled by 8
result = multiplyNum(8) # 8 becomes the num (aka argument); afterwards, 8 is multiplied by 8 to become 64.
print(result)
a = 10
b = 6.7
def subNumbers(x,y): # procedure is defined with paramters x and y
return (x-y)
result = subNumbers(a, b) # here, the result calls the procedure subNumbers and tells the procedure to subtract a and b.
print(a, "-", b, "=", result) # afterwards, it prints here
- Challenge 1 Let's try an example with a decimal to binary convertor. Please take time to create a conversion to from decimal to binary and call the procedure and print.
decimal = 7
def convertToBinary(n):
(decimal)
print()
3.13 Developing Procedures:
- Developing a procedure: What questions do I need to ask myself?
- What am I going to name my procedure? (It should reflect the purpose of the code)
- What parameters do I need?
- What data would I need to take in to accomplish my goal?
- Do I want my procedure to give a numerical value, or complete an action?
What is the purpose of this code? Is it returning a numerical value, or simply completing an action?
a = 10
b = 200
def multiplication(a, b): # function that defines a and b being multiplied
product = a * b
return product
answer = multiplication(a, b) # calls the multiplication procedure and replaces a and b with arguments that we stated at the start; a=10 and b=200.
print(a, "x", b, "=", answer)
What is procedural abstraction?
- One type of abstraction is procedural abstraction, which provides a name for a process that allows a procedure to be used only knowing WHAT it does, not HOW it does it.
- Have variable parameters
- Code handles different situations depending on how its parameters are set/called
- It allows a solution to a large problem based on the solutions of smaller subproblems.
- Separating a program's functions into independent pieces or blocks, each containing all the parts needed to execute a single aspect of the functionality is called modularity
What is a benefit of using procedures?
- We have the ability to alter the result without actually changing the calls to the program
- Convenient to change the actions if there is an error in the code (modularity)
- able to break the code up and abstract what different part of the code does; helps identiy bugs, error, etc.
- MUCH better than reviewing code WITHOUT a procedure (you would have to look at every line by line)
Here is an example of modularity and the benefits of procedures. Which is better? This is not any particulur language, but it helps us understand why procedures are beneficial
Say a object is moving across a grid and wants to get to the finish line. This is what the object is doing:
- rotate_left()
- move_forward()
- rotate_right()
- move_forward()
- rotate_right()
- move_forward()
- rotate_left()
- move_forward()
- move_forward()
Say the object doesn't end up at the finish line. It is going to be a pain to individually look at every line and see what to fix (of course, in this example, it wouldn't be that bad, but we're assuming that it will be much longer).
This is where procedures come in handy.
We can split up the code into differnet motions.
- rotate_left()
- move_forward()
- rotate_right()
- move_forward()
- rotate_right()
- move_forward()
We can call this whole segment detourRight()
- rotate_left()
- move_forward()
- move_forward()
And we can call this turnCorner()
Seperating the long code into two different procedures allows us to individually point at what might have gone wrong during the process and is therefore essential to coders.
Homework/Hacks: For the hw, you have two options, easy or hard. The easy hack is for a 0.7 + extra work for the full 1. The easy hack is simply creating your own procedure with your own creativity. Since there is a lot of leeway for this one, you must do additional work to get a 3. For the hard hack, below is the start to a character to binary convertor. This is just a template, but the goal is to translate "APCSP" into binary. The only contraint is that you must use a procedure. Doing this will get you a 1.
def charToBinary(x):