Parameters
A parameter specifies the type of an argument and the value it takes in the call to an operation. You can add parameters to the operations in your model to define the information that passes to and from the operation when calls to the operation are made. An operation can have any number of parameters or none at all.
Each parameter has a name, a type-expression, and a parameter direction type. The name of the parameter must be unique in an operation's parameter list. The type-expression is a reference to a data type, which might be a class, a primitive data type, a user-defined data type, or an interface. You can also specify a direction type and a default value for a parameter. The direction type (in, out, inout, or return) shows in which direction the parameter passes to, or from, the operation. A parameter's default value is the value it takes if you do not explicitly define a value when you call the operation. The syntax of a parameter is as follows:
[direction] name : type-expression = default-value
For example, in an e-commerce application a Customer class has a getBalance operation with the parameter [in] day : Date = 01/01/2005 parameter. The parameter syntax describes the following information:
Part of Syntax | Example | Description |
---|---|---|
[direction] | [in] | The date is passed into the operation; it is neither changed nor passed out. |
name | day | The name of the parameter (day) describes the nature of the data that passes into the operation. |
type-expression | Date | The type of value is an instance of the Date class. |
default-value | 01/01/2005 | The default value is 01 January 2005. |
You can show or hide parameters in operations. The diagram editor displays parameters in classifier shapes in several diagrams types including class diagrams, component diagrams, and use-case diagrams. An operation's parentheses enclose a comma-separated list of input and output parameters. The diagram editor displays return and throw parameters after the parentheses, and shows only their types. The Project Explorer view displays the name of the parameter under its associated operation.
The following table shows the notation for parameters in operations in the Project Explorer view and the diagram editor.
Parameter in the Project Explorer view | Parameter in the diagram editor |
---|---|
![]() |
![]() |
Example
A Customer class has a getBalance operation that returns the balance of a customer's account on a specific day. You can add a day parameter to the operation to indicate that the caller of the function must provide a date. You can add a money parameter to indicate that the operation should return an instance of the MoneyType class that specifies the amount of money in the account. To indicate that the money parameter is a return type, set its direction property to return.