3.12 Calling Procedures

Enduring Understanding

Programmers break down problems into smaller and more manageable pieces. By creating procedures and leveraging parameters, programmers generalize processes that can be reused. Procedures allow programmers to draw upon existing code that has already been tested, allowing them to write programs more quickly and with more confidence.

Learning Objective

For procedure calls:

a. Write statements to call procedures.

b. Determine the result or effect of a procedure call.

Essential Knowledge

A procedure is a named group of programming instructions that may have parameters and return values.

Procedures are referred to by different names, such as method or function, depending on the programming language.

Parameters are input variables of a procedure. Arguments specify the values of the parameters when a procedure is called.

A procedure call interrupts the sequential execution of statements, causing the program to execute the statements within the procedure before continuing. Once the last statement in the procedure (or a return statement) has executed, flow of control is returned to the point immediately following where the procedure was called.

The exam reference sheet provides procName(arg1, arg2, ...) as a way to call

Text:

PROCEDURE procName(parameter1, parameter2, ...)

{

<block of statements>

}

Block:

which takes zero or more arguments; arg1 is assigned to parameter1, arg2 is assigned to parameter2, and so on.

The exam reference sheet provides the procedure

Text:

DISPLAY(expression)

Block:

to display the value of expression, followed by a space.

The exam reference sheet provides the

Text:

RETURN(expression)

Block:

statement, which is used to return the flow of control to the point where the procedure was called and to return the value of expression.

The exam reference sheet provides

result <-- procName(arg1, arg2, ...)

to assign to result the "value of the procedure" being returned by calling.

Text:

PROCEDURE procName(parameter1, parameter2, ...)

{

<block of statements>

RETURN(expression)

}

Block:

The exam reference sheet provides procedure

Text:

INPUT()

Block:

which accepts a value from the user and returns the input value.

Last updated