Calling Procedures (College Board AP® Computer Science Principles): Study Guide

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Procedure syntax and parameters

What is a procedure?

  • A procedure is a named group of instructions that performs a specific task

  • Procedures are also referred to as methods or functions in different programming languages

  • Using procedures avoids rewriting the same code multiple times and makes programs easier to read and maintain

Calling a procedure

  • A procedure call executes the instructions inside the procedure

  • In AP CSP pseudocode, a procedure is called by writing its name followed by parentheses

PROCEDURE greet()
{
   DISPLAY("Hello!")
}

greet()
 
  • Calling greet() executes the DISPLAY instruction and outputs "Hello!"

Parameters and arguments

  • A parameter is a variable in the procedure definition that receives a value when the procedure is called

  • An argument is the actual value passed into the procedure during the call

  • Parameters allow the same procedure to work with different inputs each time it is called

  • A procedure can also collect input by calling INPUT() inside its body instead of receiving values through parameters

PROCEDURE greet(name)
{
   DISPLAY(CONCAT("Hello, ", name))
}

greet("Alex")
greet("Sam")
 
  • name is the parameter; "Alex" and "Sam" are the arguments

  • The procedure displays a different greeting depending on the argument passed

Return values and control flow

What is a return value?

  • A return value is a value that a procedure sends back to the part of the program that called it

  • Not all procedures return a value; some only perform an action such as displaying output

  • When a procedure returns a value, it can be stored in a variable or used directly in an expression

  • The RETURN statement ends the procedure and sends a value back to the caller

  • When RETURN is reached, the procedure stops executing immediately and no further statements in the procedure run

PROCEDURE square(n)
{
   RETURN(n * n)
}

result ← square(5)
DISPLAY(result)
 
  • This stores 25 in result and displays it

Sequence interruption

  • Calling a procedure temporarily interrupts sequential flow; the program jumps to the procedure body, executes it, then resumes at the point of the call

  • The effect on the calling code depends on whether the procedure returns a value:

Procedure type

Has RETURN?

Effect on calling code

Action only

No

Executes and returns; calling code continues

Value-returning

Yes

Returns a value; calling code uses it

Examiner Tips and Tricks

  • In code trace questions, track the argument values passed to each procedure call separately — a procedure called multiple times with different arguments produces different outputs each time.

  • For the CPT, your program must include a student-developed procedure with a parameter that is used within the procedure body; make sure you can explain what the parameter does in your written response.

Worked Example

What does the following code segment display?

PROCEDURE double(x)
{
   RETURN(x * 2)
}

a ← double(3)
b ← double(a)
DISPLAY(b)
 

(A) 3
(B) 6
(C) 12
(D) 24

[1]

Answer:

(C) 12 [1 mark]

  • double(3) returns 6 and assigns it to a; double(a) then passes 6 as the argument, returning 6 × 2 = 12, which is displayed

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.