Title: Functions
1Functions
2Parameter Passing
- Pass-by-value (Passing primitive data types as
parameters) - Pass the address (Passing objects as parameters)
- Returning an object (Passing objects as
parameters return an object)
3Pass-By-Value
- Some methods can be more flexible (therefore
useful) if we pass them input values - Input values for methods are called passed values
or parameters - Parameters and their data types must be specified
inside the parentheses of the heading in the
method definition - these are called formal parameters
- The calling object must put values of the same
data type, in the same order, inside the
parentheses of the method invocation - these are called arguments, or actual parameters
4Pass-By-Value (cont.) Example 1
class Circle double radius, area public
Circle()radius1 public double
findAreas(double r) double ar ar
r3.143.14 return ar public class
PassValueCircle public static void
main(String args) // Create a Circle
object with default radius 1 Circle circle1
new Circle() System.out.println(circle1.find
Areas(5.0)) System.out.println("Area
"circle1.area)
5Pass-By-Value (cont.) Example 2
 class Circle double radius, area public
Circle() radius1 public void
findAreas(double r) area
r3.143.14 public class
PassValueCircle public static void
main(String args) Circle circle1
new Circle() circle1.findAreas(5.0) System
.out.println("Area "circle1.area)
6Pass-By-Value (cont.) Example 3
class Test void tukar(int i, int j) j
ij i ij System.out.println("a dan
b dalam fungsi tukar " i " ,
"j) Â class PassValue public static void
main(String args) Â Test ob1 new
Test() int a 5, b 10 Â System.out.println("a
dan b sebelum panggilan fungsi tukar " a "
, "b)Â ob1.tukar(a,b)Â
System.out.println("a dan b selepas panggilan
fungsi tukar " a " , "b) Â
7Pass the reference
- The formal parameter must be a reference object
of the particular class itself. - The argument must be an object of the particular
class itself. - Upon the invocation of the method the reference /
address will passed as an argument to the formal
parameter of the particular method.
8Pass the reference (cont.) Example 1
class Circle double radius, area public void
findAreas(Circle r) r.area
r.radius3.143.14 public class
PassAddressCircle public static void
main(String args) Circle circle1 new
Circle() circle1.radius5.0
circle1.findAreas(circle1) System.out.println("
\n" "Radius is " circle1.radius)
System.out.println("Area "circle1.area)
Formal parameter - r is an object reference of
type Circle
Argument - circle1 is an object of type Circle
9Pass the reference (cont.) Example 2
class Square double width, height, area
Square(double a, double b) width a
height b public void findAreas(Square r)
r.area r.width r.height public class
PassAddressSquare public static void
main(String args) Square t new
Square(5.0,5.0) t.findAreas(t)
System.out.println("\n" "height " t.height
"\twidth" t.width) System.out.println("Area
"t.area)
10Pass the reference (cont.) Example 3
class Test int x, y  Test (int no1, int
no2) x no1 y no2 void tukar(Test
ob) ob.x ob.x ob.y ob.y ob.y ob.x
System.out.println("ob.x dan ob.y dalam
fungsi tukar " ob.x " , "ob.y) class
PassObjek public static void main(String
args) //constructor digunakan untuk memberi
nilai awal kepada objek ob Test ob new
Test(5,10) System.out.println("a dan b sebelum
panggilan fungsi tukar "ob.x" "ob.y) Â //
objek ob sebagai parameter ob.tukar(ob)
System.out.println("a dan b selepas panggilan
fungsi tukar "ob.x" "ob.y)
11Pass the reference (cont.) Example 4
class Rnum private int num,denom
Rnum(int a, int b) num a denom b Â
public void print() System.out.print(num
"/" denom) Â public void add(Rnum
ob1, Rnum ob2) num ob1.num ob2.denom
ob1.denom ob2.num denom ob1.denom
ob2.denom public static void
main(String args) Rnum num1 new
Rnum(5,10) Rnum num2 new Rnum(3,4) Rnum
num3 new Rnum(0,0) Â num1.print() System.out.
print(" ") num2.print() num3.add(num1,num2)
System.out.print(" ") num3.print()
12Return an object
- A formal parameter consists of an object
reference of the particular class. - An argument consists of an object of the class
itself. - The method must have a return type of class.
- The method will return an object.
- Syntax for the method
- ClassName FunctionName (ClassName
ObjectReference) -
- //statements
- return ObjectName
13Return an object (cont.) Example 2
class Complex private float x,
y Complex(float u, float v) x u y
v public Complex sum(Complex c1, Complex
c2) //local object declared Complex
c3 new Complex(0,0) c3.x c1.x c2.x
c3.y c1.y c2.y return
c3 public void show(Complex object)
System.out.println("Nilai x adalah "
object.x) System.out.println("Nilai y
adalah " object.y)
14Return an object Example 2 (cont.)
public static void main(String args) Complex
ob1 new Complex(3,5) Complex ob2 new
Complex(5,5) Complex ob3 new Complex(0,0)
? ob3.sum(ob1,ob2) System.out.printl
n("Nilai bagi ob1 ") ob1.show(ob1) System.out.p
rintln("Nilai bagi ob2 ") ob2.show(ob2) System.
out.println("Nilai bagi ob3 ") ob3.show(ob3)
15Return an object Example 3
class Time private int hours, minutes
Time(int h, int m) hours h minutes
m public void showTime( )
System.out.println(hours " hours and "
minutes " minutes ") public void
sum(Time m1, Time m2) //no local object
declared minutes m1.minutes
m2.minutes hours minutes/60 minutes
minutes60 hours hours m1.hours
m2.hours //no returned object
16Return an object Example 3(cont.)
public static void main(String args )
Time t1 new Time(2,45) Time t2 new
Time(3,30) Time t3 new Time(0,0) t3.sum(t1,
t2) System.out.print("Masa T1 adalah
") t1.showTime() System.out.print("Masa T2
adalah ") t2.showTime() System.out.print("Masa
T3 adalah ") t3.showTime()