Function and Procedure Calls
A function call has the form
<function-name> ( <arguments> ) // this is an expression
and a procedure call has the form
<procedure-name> ( <arguments> ) ; // this is a statement
where <arguments> is a sequence of expressions separated by commas.
The method call may have to be preceded by an expression that indicates where the method is. For example,
- Math.sqrt(7) calls static function sqrt in class Math.
- s.length() calls function length of the object whose name is in variable s.
- s.substring(3,5).length() first calls the two-parameter function substring of the object whose name is in s to produce a second String object and then calls function length of that second object.
The number of arguments must equal the number of parameters of the method being called, and each argument type must be the same as or narrower than the corresponding parameter type.
A method call is executed as follows —this is explained in more detail in a later lecture:
- Create the parameters and local variables of the method being called.
- Assign the values of the arguments to the corresponding parameters.
- Execute the method body.
- Erase the parameters and local variables —and, if this is a function, return the value of the expression of the return-statement that terminated the method body.
From the above, you can see that the arguments are evaluated once and their values are stored in the parameters. In the method body, storing a value in a parameter has no effect on the corresponding argument.