Introduction to Computer Science - Java
Methods ( or Functions )
- "Black Box" metaphor
- Scope of variables - variables are only recognized inside the block of code where they
are defined. Thus a variable defined within a nested block of code is not known outside the inner nest.
The same hold true for methods. Varables in one method are not known by variables in other methods. The
variables defined by the method are not known by the calling method ( for example main() ).
So how can we pass values into methods and get values out of methods?
- Parameters are the list of values as comma deliniated variables we can pass into the method. We can
have as many parameters (inputs) as we want. Although it is true that these variables are defined
locally ( i.e. within a method ) the values they get are equal to the values the
calling function supplies in it argument list.
The order of the list determines which values are assigned to which varaibles. For example, the method
myMethod( int a, int b, float c ) has three parameters, the first two
are integers and the last one is a float. If I were to pass it the values 1 , 2, and 7.1234 to the method like this
myMethod( 1, 2, 7.1234 )
then a would get the value 1, b would get the value 2, and c would
get the value 7.1234. Order determines which value is assigned to which varaible.
- Method call ( function call ) - when I try to run a method I simply write the name of the method
followed by ( ). inside the ( ) I place the list of parameters I want to pass to the method. This is
called a method call.
- Return values - If a method outputs some value when it is done, this is called a return value.
To return a value, use the key-word return followed by the
value you wish to return. This
statement occurs inside the method and causes the method to exit immediately.
The return value is sent back to the place where the method was called. If there was an assignment operator
there for example, the variable being assigned will get the value the method returns.
For example: int a = myMethod( 1, 2, 7.1234) .
a will be assigned the value myMethod returns. Methods can return only one value. You must specify
the type value the method returns, for example int or float or String.
- Method definition - Writing the method is called defining it. Some methods are built-in, but the
programmer may write his own methods. To do this, he must define the method. That is, he must
define what return value type he intends to return, what paramenters he can accept, and of course
what the method will actually do with the values it receives when it is called.
Here is a method definition:
float myMethod ( int a , int b , int c,
return ( (
)a * ( float
)b * ( float )c / d );
- Method overloading (not on midterm)
© Nachum Danzig November 2003