1.5.4 Representing Returned Values in Sequence Diagrams

Overview

A sequence diagram shows how participants interact over time. For a method that returns a value, show a call message going to the object and a return message coming back to the caller.

Consider the C# statement:

int seatsLeft = flight.GetAvailableSeats();

The caller asks the Flight object to run GetAvailableSeats(). The Flight object calculates the number of unbooked seats and returns it. The caller stores that result in seatsLeft.

MainWindow calls GetAvailableSeats on Flight and receives the integer 34 before assigning seatsLeft.
Call and Return for Available Seats
  • New
  • Changed
  • Removed

Read a sequence diagram from top to bottom:

  1. The caller sends the GetAvailableSeats() message to flight: Flight.
  2. The Flight object executes the method using its own seat counts.
  3. The Flight object sends the returned integer back to the caller.
  4. The caller can use that integer after the reply.

Label the outgoing call with the method name and its arguments, if any. Label the reply with the value or its meaning, such as availableSeats: int. A returned value is usually drawn as a dashed arrow pointing back toward the caller.

Read the Call and Return

Suppose the Flight has capacity 120 and 86 booked seats. The call message is GetAvailableSeats(); the reply carries 34. A diagram could label that reply availableSeats = 34 to make the example concrete.

Messages in this interaction
Order Sender Receiver Label Meaning
1 Caller flight: Flight GetAvailableSeats() Ask the Flight to calculate open seats.
2 flight: Flight Caller availableSeats = 34 Return the calculated integer.

The assignment to seatsLeft occurs on the caller's side after the reply. It is not a separate request to the Flight object.

The class diagram records the method's general contract as GetAvailableSeats(): int. A sequence diagram describes one call, so it may show the particular result 34. If the same method is called later after more seats are booked, the return type is still int, but the returned number may differ.

In C#, return sends a value back. In the class diagram, the type after the parentheses promises what kind of value comes back. In the sequence diagram, the reply shows that value moving from the object to its caller.