πŸ“š

Β >Β 

⌨️ 

Β >Β 

πŸ“±

3.12 Calling Procedures

2 min readβ€’june 18, 2024

Minna Chow

Minna Chow

Milo Chang

Milo Chang


AP Computer Science Principles ⌨️

80Β resources
See Units

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:
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-q90PTGDIEoMB.png?alt=media&token=62d7cb04-25d5-47b9-ac69-e69aaba29ca0
Let's go a little into what this means.

Parts of a Procedure:

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.
https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-fES1sHFGiN43.png?alt=media&token=cb30094a-5353-47d4-8c76-c3be227ad85c
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.

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.