6.1: Method Syntax in Java
Review these lecture notes.
A method must have a name but may not have parameters and a return type. Pages 1 - 2 explain how a method is declared in Java.
The parameter list contains variables that receive values from the caller. These variables are local to the method. Method parameters are also referred to as 'arguments' when they are substituted with specific values during the method call. Method parameters are explained on page 2.
Some methods do not return any value, while others return a value of certain data type. A method that does not return a value uses the keyword 'void' in the method signature. Value returning methods are illustrated on page 2 and non-value returning methods are illustrated in the example on page 3.
The scope of a variable refers to where the value of the variable is accessible or valid. A variable declared inside a method is only available in that method. The values of variables declared as parameters of a method are valid only inside that method as well. Read pages 4, 5, and 6 to learn about the scope of a variable.
The first video implements factorial as a function. Please note that a 'method' is often referred to as 'function' in some programming languages. The above resource uses the term 'function' instead of 'method' as the concepts are explained using Python.
The second video verifies the function design by manually diagramming what happens statement by statement and function call by function call. This lecture also introduces variable scoping.
The third video introduces recursion by implementing the factorial function using recursion. If you are familiar with proof by induction in mathematics, you will notice its conceptual similarity with recursion. Some verification is done by running the program for several cases.
The fourth video compares the loop implementation with the recursive implementation of the factorial function by stepping through them, side by side, using the same example inputs.
Please note that though these video lectures use Python programming language to explain methods/functions, the basic idea of method and method call is the same in Java programming language.