A procedure is a group of programming instructions. They're also known as methods or functions, depending on the programming language. You can use a procedure to use the same set of instructions, again and again, without having to rewrite it into your code.
In AP Pseudocode, here's how they'll represent procedures:
Let's go a little into what this means.
Let's go back to this example we used earlier for a program that sums two numbers.
first_number = 5
second_number = 7
sum_value = first_number + second_number
print (sum_value)
It goes without saying that, if you wanted to sum more than one set of numbers at a time, you'd have to rewrite these lines over and over again...
Let's see how we'd go about writing this as a function, or a procedure in Python.
A function in Python looks like this:
def name_of_function (parameter1, parameter2):
#code statements here
So our summing machine would look like this!
def summing_machine(first_number, second_number):
print (first_number + second_number)
What are those parentheses for? Procedures often require some sort of parameter in order to work. Parameters are the input variables of a procedure. In this case, the parameters are the two numbers you want to add together: first_number and second_number.
β You don't always need to use parameters in a procedure.
When you call a procedure, your program acts like those lines of code are written out. Once it's done executing those code lines, your program goes back to reading code sequentially.
When you call that procedure with defined values or variables, those values are known as arguments.
def summing_machine(first_number, second_number):
print (first_number + second_number)
summing_machine(5, 7)
#In this example, 5 and 7 are examples of arguments.
If you have a procedure in a longer chunk of code and you want to exit the function, you can use the return statement, in both Python and Pseudocode. This will return the value of an expression that you can then use, without needing to print it.
For example:
def summing_machine(first_number, second_number):
value = first_number + second_number
return (value)
answer = summing_machine(5, 7)
print (answer)
#you can use this to manipulate the value the function returns as well.
print (answer + 1)
Note that the Pseudocode allows you to assign this returned value to a variable using an arrow.