0% found this document useful (0 votes)
1 views26 pages

Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views26 pages

Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Functions

Functions:
 In Java, all function definitions must be inside
classes.
 Functions also called as methods.
 Method is a collection of statements that are
grouped together to perform an operation.
Syntax:

• return_type is the value to be returned to an calling


method.
• method_name is a name of the method ( function).
• arg1, arg2, arg3 are the different parameters that
which are pass to a method.
Return Type of Method :
Method can return any type of value.
 Method can return any Primitive data type
means int or float etc..
Ex:
int sum (int num1, int num2);
 Method can return Object of Class Type.
Ex:
Rectangle sum (int num1, int num2);
Cont…
 Method sometimes may not return value.
Ex:
void sum (int num1, int num2);
Method Name

 Method name must be valid identifier.


 All Variable naming rules are applicable for
writing Method Name.
Parameter List :
 Method can accept any number of
parameters.
 Method can accept any data type as
parameter.
 Method can accept no Parameter.
Example:
class Rectangle
{
int length;
int breadth;
void setLength(int len)
{
length = len;
}
int getLength()
{
return length;
}
}
class RectangleDemo
{
public static void main(String args[])
{
Rectangle r1 = new Rectangle();
r1.setLength(20);
int len = r1.getLength();
System.out.println("Length of Rectangle : " + len);
}
}
Output:
Parameter Passing
 The two most prevalent modes of passing
arguments to methods are “passing-by-value”
and “passing-by-reference”.

 As far as Java is concerned, everything is


strictly Pass-by-Value.
Cont….
 If a function B() is called from another
function A().
 In this case A is called the “caller
function” and B is called the “called function
or callee function”.
 The arguments which A sends to B are
called actual arguments and the parameters
of B are called formal arguments.
Example:
Methods of Parameter Passing
 Pass By Value
 Pass-by-Reference
Pass By Value:
 When a parameter is pass-by-value, the caller
and the callee method operate on two
different variables.
 which are copies of each other.
 Any changes to one variable don't modify the
other.
Cont…
 Parameters passed to the callee method will
be clones of original parameters.
 Any modification done in callee method will
have no effect on the original parameters in
caller method.
Example
Output
Pass-by-Reference
• When a parameter is pass-by-reference, the
caller and the callee operate on the same
object.
• It means that when a variable is pass-by-
reference, the unique identifier of the object
is sent to the method.
• Any changes to the parameter’s instance
members will result in that change being
made to the original value.
Example
Output
 In Java, Primitive variables store the actual
values.
 Non-Primitives store the reference variables
which point to the addresses of the objects
they're referring to.
In case of call by value original value is not changed.
In case of call by reference original value is changed if
we made changes in the called method
Static Modifier

 The static modifier is associated only with


methods and variables, not classes.
 The static modifier is used to specify a method
that can only be declared once.
 No subclasses are allowed to implement a
method of the same name.
 This is used for methods, such as main, that
are entry points into a program.
 Static methods are used to specify methods
that should not be overridden in subclasses.
static void is Black (Color color)

{
....
}
An example to demonstrate the differences
between static and public methods
Output:
Static methods can be called without creating objects
Public methods must be called by creating objects

You might also like