Functions
Functions
Method declarations
Methods
class MyClass
{
static int min ( int num1, int num2 )
class MyClass
{
static int min(int num1, int num2)
{
int minValue = num1 < num2 ? num1 : num2;
return minValue;
}
}
The return Statement
The return type of a method indicates the
type of value that the method sends back to
the calling code.
An immediate exit from the function is
caused as soon as a return statement is
encountered.
10
A function may contain several return
statements, however only one of them
gets executed because the execution
of the function terminates as soon as a
return is encountered.
{
int minValue = (num1 < num2 ? num1 : num2);
return minValue;
}
Method Control Flow
A method can call another method, who
can call another method,
main
min(num1, num2, num3) println()
min(1, 2, 3);
println()
More Examples
double tryMe ( int x )
{
return x + 5;
Which tryMe will be called?
}
tryMe( 1 );
double tryMe ( double x )
{ tryMe( 1.0 );
return x * .375;
} tryMe( 1.0, 2);
tryMe( 1, 2);
double tryMe (double x, int y)
{ tryMe( 1.0, 2.0);
return x + y;
}
Variable Scoping
Variables
At a given point, the variables that a statement can
access are determined by the scoping rule
the scope of a variable is the section of a program in which
the variable can be accessed (also called visible or in scope)
Pure
functions are those that do not
change the state of their parameters.
Pure function takes objects and/or
primitives as arguments but does not
modify the objects
Impure Function
Impure functions are those that
change/ modify the state of their
parameters. Impure functions are
also called modifier functions.
Java Scoping Rule
class/static variable: a variable defined in
class scope and has the static property
it is associated with the class
and thus can be accessed (in scope) in all
methods in the class
instance variable: a variable defined in class
scope but not static
it is associated with an instance of an
object of the class,
and thus can be accessed (in scope) only
in instance methods, i.e., those non-static
methods
Actual Parameters: The values
are passed with the defined
parameters in a function , called
actual parameters.
}
A variable with a block scope
can be accessed in the enclosing block;
also called local variable
a local variable can shadow a variable in a
class scope with the same name
local variables
those declared in the method
formal arguments
Example1:
public class Box
{
private int length, width;
}
Scope of Variables
public class Box
{ Instance variables
private int length, width;
are accessible in all
methods of the class
public int widen (int extra_width)
{ formal arguments
private int temp1;
size += extra_width; are valid within
}
their methods
public int lenghten (int extra_lenth) Local variables are
{
private int temp2; valid from the point
size += extra_length;
of declaration to the
} end of the enclosing
block
}
Two Types of
Parameter Passing
If a modification of the formal argument
has no effect on the actual argument,
it is call by value
34
Defining Classes
A class contains data declarations (state)
and method declarations (behaviors)
class Month
int month;
int year Data declarations
Method declarations
Method Overloading
A class may define multiple methods with the same
name-, but different parameters ,this is called method
overloading
usually perform the same task on different data types
Invocation
Invocation