0% found this document useful (0 votes)
5 views

Function Overloading

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Function Overloading

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

A function can be sent parameters/arguments for work in two ways :

1) Call by Value : In this case the function copies the actual variables values
inside
its own copy/dummy variables and works with them. Any changes made to the
copy
variables does not affect the original values in the variables in the
caller.This technique is known as call by value.
Any function working in Call By Value technique is known as pure function
because this type of function does not changes the actual variables' values in the
caller.
The copy variables in the called function are known as FORMAL PARAMETERS and
the
actual variables in the caller are known as ACTUAL PARAMETERS.
** Whenever any primitive datatypes are passed to a function as parameter
always
call by value is implemented. (byte , short , int , long , char ,
float,
double , boolean)

class test
{
public void change(int a) // 'a' is the formal parameter
{
System.out.println("The value on entering the function is : "+a);
a=a+20;
System.out.println("After change the value in function is : "+a);
}
public void main()
{
int p=10; // actual parameter
System.out.println("The value in main is : "+p);
change(p); // main calls the function change()
System.out.println("After the function worked value in main is : "+p);
}
}

Output :

The value in main is : 10


The value on entering the function is : 10
After change the value in function is : 30
After the function worked value in main is : 10

***********************************************************************************
*

ex : 2
========

class test
{
public void swap(int a,int b) // 'a' and 'b' are the formal parameters
{
System.out.println("The values on entering the function is : "+a+" , "+b);
int c=a;
a=b;
b=c;
System.out.println("After change the values inside function is : "+a+" ,
"+b);
}
public void main()
{
int p=10,q=35; // actual parameter
System.out.println("The value in main is : "+p+" , "+q);
swap(p,q); // main calls the function swap()
System.out.println("After the function worked values in main are : "+p+" ,
"+q);
}
}

Output :

The value in main is : 10 , 35


The values on entering the function is : 10 , 35
After change the values inside function is : 35 , 10
After the function worked values in main are : 10 , 35

-----------------------------------------------------------------------------------
-----

2) Call by Reference : In this technique the called function directly works on


the
actual variables inside the caller and no copies are made. So any changes
made
inside the function directly affects changes on the actual variables.
** Any non-primitive types if send to a function always works in this
technique.
(String , array , object , interface etc...)
The functions working in Call by Reference technique is known as Impure
Function.
This is because the function changes the actual variables.

class test
{
public void change(String a) // 'a' is the reference parameter
{
System.out.println("The value on entering the function is : "+a);
a="Akshat";
System.out.println("After change the value inside function is : "+a);
}
public void main()
{
String t="Utsav"; // actual parameter
System.out.println("The value in main is : "+t);
change(t); // main calls the function swap()
System.out.println("After the function worked value in main is : "+t);
}
}

ex : 2
=======

class test2
{
String d="Shristi";
public void change(test2 t)
{
System.out.println("The value on entering the function is : "+t.d);
t.d="Swati";
System.out.println("After change the value inside function is : "+t.d);
}
public static void main(String args[])
{
test2 ob=new test2();
System.out.println("The text in the object is : "+ob.d);
ob.change(ob);
System.out.println("Inside main the value is : "+ob.d);
}
}

Output ::

The text in the object is : Shristi


The value on entering the function is : Shristi
After change the value inside function is : Swati
Inside main the value is : Swati

-----------------------------------------------------------------------------------
----

Function Overloading :

This is a part of polymorphism whereby an object can react or work with more than
one ways with its functionability.

class shape
{
public void area_sq(int s) // 8 bytes space
{
int a=s*s;
System.out.println("The area of square is : "+a);
}
public void area_rect(int l,int b) // 12 bytes space
{
int a=l*b;
System.out.println("The area of rectangle is : "+a);
}
public void area_cir(double r) // 16 bytes space
{
double a=3.14 * r * r;
System.out.println("The area of circle is : "+a);
}
public static void main(String args[])
{
shape sh=new shape();
sh.area_cir(5.25);
sh.area_sq(7);
sh.area_rect(4,5);
}
}

===================================================================================
=========
In case of function overloading we create a single function with different versions
or
definations. The difference will be the number and type of parameters sent to the
function. The function name must remain the same.
The advantage of this is saving memory space and using a same memory space for
loading the different versions of the funtion and working.

class shape
{
void area(int s) // 8 bytes
{
int a=s*s;
System.out.println("The area of square is : "+a);
}
void area(double r) // 16 bytes
{
double a=3.14 * r * r;
System.out.println("The area of circle is : "+a);
}
void area(int l,int b) // 12 bytes
{
int a=l*b;
System.out.println("The area of rectangle is : "+a);
}
void input()
{
....
}
void display()
{
.....
}
}

ex :
======

class shape
{
public void area(int s)
{
int a=s*s;
System.out.println("The area of square is : "+a);
}
public void area(int l,int b)
{
int a=l*b;
System.out.println("The area of rectangle is : "+a);
}
public void area(double r)
{
double a=3.14 * r * r;
System.out.println("The area of circle is : "+a);
}
public static void main(String args[])
{
shape sh=new shape();
sh.area(5.25);
sh.area(7);
sh.area(4,5);
}
}

You might also like