Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CH – 5

USER – DEFINED METHODS


Function definition – A program module used at different instances in a program to perform a
specific task is known as a function or method.

Advantages of a method / function –

 It divides complex task into a collection of smaller methods.


 A program that uses function, occupies less memory space and executes faster.

Method header / Function prototype – heading of a function

Syntax – <access specifier><return type><method name> (parameter list)

eg- public void sum(int a, int b) here the data type of variables should be mentioned

Function signature – It is identification of a function. It is written by using the method name


along with the parameters to be passed.

eg – add (a,b) here the data type of variables should not be mentioned

Access specifier – determines the scope of usage / availability of a function.

eg – public, private, protected

Return type – it is the data type of the result of a function to be returned to its caller.

Return statement – A statement that sends back the result from a function to its caller
program. It can only return a single value from a method. It is always used at the end of a
method.

Differentiate between -

Public Private Protected

Scope of a public
Scope of a private Protected method/member
method/member is global. ie, it
method/member is within the can be accessed by inherited
can be accessed from external
class classes only
classes also

Default access specifier Not a default access specifier Not a default access specifier

Static method Non-Static method


No need of an object for its calling Need an object for its calling
p = sum(); p = ob.sum();

main() and sub methods are not


main() and sub methods are compatible
compatible to communicate with each
to communicate with each other
other
Actual Parameters Formal Parameters
Values actually passed to the method at The parameters described in the method
the time of its call from the main() header which receive the values from the
function caller method
Found in function body Found in function header
ob.sum(a,b); int sum (int x, int y) { }
Here a and b are actual parameters Here x and y are formal parameters

Call by value Call by reference


The method creates its new set of formal Reference of the actual parameters is
parameters to copy the value of actual passed on to the method. No new set of
parameters and works with them variables is created.

Any change made in the formal


Any change made in the formal
parameter is not reflected in actual
parameter is reflected in actual parameter
parameter

Primitive data types are passed Reference data types are passed

Note-
import java.util.*;
class fn1
{
public void sum( int a1, int b1)
{ // formal parameters
int c=a1+b1;
System.out.println("Sum = "+c);
}
public static void main (String args[])
{
Scanner in = new Scanner(System.in);
int a, b;
System.out.println("Enter the value of a & b");
a=in.nextInt();
b=in.nextInt();
fn1 ob =new fn1();
ob.sum( a,b); // call by value or pass by value
}
} // actual parameters

What are the different ways of defining / forms of functions?

 Receiving values and returning outcome to the caller


 Receiving values but not returning outcome to the caller
 Neither receiving values nor returning outcome to the caller
What are the different ways to invoke a function?

 Call by value / Pass by value


 Call by reference / Pass by reference

What are the types of function?

 Pure function
 Impure function

Pure function Impure function

Doesn’t change state of an object Changes the state of an object


Returns a value to its caller module May not return a value to its caller module
Also known as Accesser method Also known as Mutator

Function Overloading – process of defining functions with the same function names but with
different numbers or types of parameters

Static Binding – finding the best match of the function arguments (types and number of
parameters) on invoking a function is known as early binding or static binding

Recursive function – A function designed in such a way that it calls itself in its body.

_____________________

You might also like