Subroutine Return Values
A formal parameter is a named slot in a subroutine definition, with a simple identifier as its name and a declared type.
A Call That Looks Suspicious
Suppose you write a subroutine that expects a decimal number, and later you call it with a whole number. Does the call work, or does Java reject it? The answer depends on a rule that governs every subroutine call. This article builds that rule from the ground up, so keep your first guess in mind while you read.
What do you think happens?
A subroutine's formal parameter is declared as a double, and you call the subroutine with the whole number 7. Does the call work?
Reveal answer
Yes, the call works.
The source states that an int may be passed where a double is expected, because an int can be assigned to a double. The rule that decides this is the assignment rule, which the rest of this article develops.
Two Kinds of Parameter
The word parameter is used for two related things that are easy to confuse. One is the list of names written inside a subroutine's header. That list describes what information the subroutine needs. The other is the list of values written inside a call, which supplies that information. The first role belongs to the definition, and the second belongs to the place where the subroutine is used. Both roles are tied together by the same subroutine, so the two kinds of parameter always come in pairs, even though they live in different parts of a program.
A formal parameter is a named slot in a subroutine definition. Its name is a simple identifier, and it carries a declared type. Each formal parameter has a declared type such as int, boolean, String, or double[]. A formal parameter is a stand-in rather than a real piece of data, which is why it must be written as a name. An expression such as total + 2 is not allowed in this position, because the definition is describing a slot and not computing a result.
An actual parameter is the value you hand to the subroutine at the moment you call it. Because it is a value, it can be written as any expression that produces a value of the right type. A plain variable, a literal, or a calculation all qualify. The source's own example is the variable K in the statement print3NSequence(K);, where K is the actual parameter for the formal parameter in that subroutine's definition.
A formal parameter is a named slot in a subroutine definition, with a simple identifier as its name and a declared type. An actual parameter is a value supplied in a subroutine call and may be any expression that produces a value of an acceptable type.
Tracing a Call Step by Step
Parameter passing is the process that connects the two roles, and it follows a fixed sequence. That sequence is what makes the behavior predictable. First, the actual parameters in the call statement are evaluated, so any expressions are reduced to concrete values. Second, those values are assigned to the matching formal parameters in the definition. Only after that assignment is complete does the body of the subroutine begin to run.
Read the diagram from top to bottom as a timeline. The call statement triggers evaluation first. The value 7 is then copied into the formal parameter amount. The body runs last, and it sees amount already holding that value. Because the assignment happens before the body starts, the body never has to deal with an unfilled slot.
The Assignment Rule for Acceptable Values
The rule that decides whether a value fits a slot is assignability. The value must be one that could legally be assigned to the formal parameter with an assignment statement. The call does not require the types to match exactly. You can test compatibility by imagining the ordinary assignment statement. Ask whether the formal parameter could receive the actual value on its own line of code, such as amount = 7; for a double amount. If that assignment would be legal, the call is legal too.
- double amount is the formal parameter: a name plus a declared type, written in the definition
- 7 is the actual parameter: a value supplied at the call site
- The int 7 is acceptable because the assignment double amount = 7; is legal
Checking whether showAmount(7) is legal
The definition is static void showAmount(double amount). Is the call showAmount(7); acceptable?
- 1Identify the formal parameter
The formal parameter is amount, declared as double.
- 2Identify the actual parameter
The actual parameter is 7, a whole-number value of type int.
- 3Apply the assignment rule
Imagine the statement amount = 7; If this assignment is legal, the call is legal.
- 4Check the assignment
The source states that an int can be assigned to a double, so the assignment is legal.
- 5Conclude
The actual parameter is acceptable, and the call is legal.
showAmount(7); is acceptable for the formal parameter double amount.
Common Mistakes With Parameters
Common mistake Writing an expression in the definition where a name belongs
What it looks likestatic void show(total + 2)Why it failsThe definition describes a slot, and a formal parameter must be a simple identifier with a declared type, not a computed value.
Do this insteadstatic void show(double total)Common mistake Confusing the two kinds of parameter
What it looks likeCalling the value in the call 'the formal parameter'
Why it failsThe formal parameter appears in the definition's header; the actual parameter is the value supplied in the call.
Do this insteadCall the name in the header the formal parameter and the value in the call the actual parameter.
Common mistake Assuming the types must match exactly
What it looks likeExpecting a call with an int to fail for a double formal parameter
Why it failsThe call needs the actual parameter to be assignable, and an int can be assigned to a double.
Do this insteadApply the assignment rule instead of demanding an exact type match.
Common mistake Supplying the wrong number of actual parameters, or placing them in a different order than the formal parameters
Testing Candidate Values Against a Slot
The same assignment test works for any candidate actual parameter. Write the formal parameter's declared type on the left, the candidate value on the right, and ask whether the assignment would be legal. The table below applies that test to a formal parameter declared as double. Only the first row is taken from the source's illustration. The remaining rows are a worked application of the same rule.
| Formal parameter | Candidate actual parameter | Assignment to test | Acceptable? |
|---|---|---|---|
| double amount | 7 (int) | amount = 7; | Yes, the source states an int can be assigned to a double |
| double amount | a double value such as 3.5 | amount = 3.5; | Yes, the value is already of the formal parameter's type |
| double amount | a calculation such as 2 * 4 | amount = 2 * 4; | Yes, the expression produces a value that can be assigned to a double |
Applying the assignment rule to a double formal parameter
Practice Judging Parameter Compatibility
A subroutine is defined as static void setCount(int count). Would the call setCount(2.5); be acceptable? Explain your answer by writing the assignment statement you would test and saying whether it is legal.
Hints
- Identify the formal parameter first: count, declared as int.
- Imagine the assignment count = 2.5; and ask whether it is legal.
- Compare this direction with the source's example, where an int was passed to a double.
Key Takeaways
- A formal parameter is a named slot in a subroutine definition, with a simple identifier as its name and a declared type; an actual parameter is a value supplied in a subroutine call and may be any expression that produces an acceptable value.
- A call first evaluates the actual parameters, then assigns each value to its matching formal parameter, and only then executes the body.
- An actual parameter is acceptable when it could legally be assigned to the formal parameter, so you decide compatibility by imagining the assignment statement; an int may be passed where a double is expected.
- Each call must supply exactly one actual parameter for each formal parameter, in the same order.