• A function is a static method
• Implement mathematical functions
• Can have strings & other data types as their range/domain &
can produce side effects (e.g. printing output)
• Whenever you can separate tasks within programs (into
methods), you should do so
• Methods control flow
• Define static methods in a .java file by specifiying
o Method signature
o Sequence of statements that constitute the method
Control flow
• Harmonic has 2 static methods → harmonic() and main()
• First statement that Java executes is the first statement in main()
• “Call” = harmonic(arg)
• Next few statements operate as usual, except that the code
harmonic(arg) causes a “transfer of control” to the first line of
code in harmonic() each time it’s encountered
• Parameter variable n in harmonic() initialized to value of arg in
main() at the time of the call
• Executes statements in harmonic() until it reaches a return
statement → control back to main()
• Return value assigned to variable value
Function-call trace
• Approaches to following control flow
o Imagine each function prints its name &
argument value(s) when it’s called
o Added indentation exposes flow of control &
helps us check each function has the effect
we expect
• Adding calls on StdOut.println() helps understand
what a program is doing (debugging)
• If return value match expectations, we don’t need to
tract the function code in detail → saves us work
1
, Terminology
Static method definition
• Signature = first line of definition
• Also specifies type of each parameter variable & return type of
method
• Return statement transfers control back to point where static
method was called & returns result
• Body may declare local variables (used only in the method
they’re declared in)
Function calls
• Method call = method name + arguments → separated by commas & in
parentheses (same form for math functions)
• Method call is an expression (can use it to build more complicated
expressions)
• Argument is an expression
Multiple arguments
• Static method can take more than one argument
& therefore more than one parameter variable
• Parameter variables are generally different Multiple return statements
types • Control goes back to calling program as soon as
• Type & name of each parameter variable are first return statement is reached
declared in the function signature (declarations • May be multiple return statements → any static
for each variable separated by commas) method returns a single value each time it’s
invoked (value after first return statement
Multiple methods encountered)
• Some programmers insist only having one
• Can have many static methods in a .java file
return statement per method (but not strict in
• Each method has a body with sequence of
this textbook)
statements enclosed in curly braces
• Methods are independent & can be in any order
in the file
• Static method can call any other static method Single return value
in the file/in a Java library • Java method provides only 1 return value to the
caller of type declared in method signature
Overloading • Java data types can contain more info than the
• Static methods with different signatures are value of a single primitive type
different static methods
• Overloading = using same name for different
methods → common practice in Java programs
• Another use = define 2 versions of a method (1
takes an argument & 1 uses a default value for
that argument)
2