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.
Read a sequence diagram from top to bottom:
GetAvailableSeats() message to flight: Flight.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.
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.
| 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.