0% found this document useful (0 votes)
14 views5 pages

User-defined Methods in Programming

Chapter 3 discusses user-defined methods in programming, emphasizing their importance for code reusability and organization. It covers method syntax, types (parametrized and non-parametrized), method calling techniques (call by value and call by reference), and the concepts of pure and impure functions. Additionally, it introduces constructors, their properties, and the concept of constructor overloading.

Uploaded by

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

User-defined Methods in Programming

Chapter 3 discusses user-defined methods in programming, emphasizing their importance for code reusability and organization. It covers method syntax, types (parametrized and non-parametrized), method calling techniques (call by value and call by reference), and the concepts of pure and impure functions. Additionally, it introduces constructors, their properties, and the concept of constructor overloading.

Uploaded by

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

CHAPTER-3 (User-defined Methods)

Need of methods- It allows code reusability (define once and use multiple times) You can break a complex
program into smaller chunks of code. Methods are also called as Functions.

Syntax (Prototype/Method definition for writing methods):

Access Specifier Return type Method name (Parameter/Argument list)


{
Body of method;
}

• Access Specifier- public, private & protected but mostly we will use public.
• Return type- void or any data type (byte, short, int, long, float, double, char, boolean, String). Here
void means that no value(output) is coming out of a method for storing in memory while if any
return type is mentioned, it defines a value will be coming out of a method and being stored in
memory.
• Method name- name of a method by using rules of identifiers.
• Parameter/Argument list- These means the variables (for input purpose only) required for
working in a method. This can be filled as well as empty.
• Methods are of two types:
i. Non-parametrised Methods – Means empty parameter list
ii. Parametrised Methods- Means filled parameter list
• Method signature- Method name with Parameter list

Examples: - Method name is area ( )

a. A method with no return type and no parameter-


public void area ( )
{
Body of method;
}
b. A method with return type and no parameter-
public int area ( )
{
Body of method;
return (output variable);
}
c. A method with no return type and with parameter-
public void area (int l, int b)
{
Body of method;
}
d. A method with return type and with parameter-
public int area (int l, int b)
{
Body of method;
return (output variable);
}

These above way of creating methods is called as method definition. No method works unless and until
method calling / method invoking is done. There are two types of method calling/invoking-

i. Call by value/Pass by value ii. Call by reference/Pass by reference


All four variation of method definition with their method calling is shown below:

Example:

i. class Rectangle
{
public void area ( )
{
Scanner sc = new Scanner ([Link]);
[Link](“Enter length & Breadth”);
int l=[Link]( );
int b=[Link]( );
int ar = l*b;
[Link](“Area of rectangle=”+ar);
}
public static void main (String args[])
{
Rectangle ob = new Rectangle( );
[Link]( ); Here, in method calling, the method is
} called by using dot operator with object.
}
.
ii. class Rectangle
{
public void area (int l, int b)
{
int ar = l*b;
[Link](“Area of rectangle=”+ar);
}
public static void main (String args[])
{
Rectangle ob = new Rectangle( );
Scanner sc=new Scanner ([Link]);
[Link](“Enter the length and breadth”);
int x= [Link]( );
Here, in method calling variables x, y are used
int y= [Link]( );
[Link](x,y); for passing the input values to the arguments l, b.
} So x, y are called as actual parameters and l, b
} are called as formal parameters.
iii. class Rectangle
{ *Variable z is not an actual parameter.
public int area (int l, int b)
{
int ar = l*b;
return (ar);
}
public static void main (String args[])
{
Rectangle ob = new Rectangle( );
Scanner sc=new Scanner ([Link]);
[Link](“Enter the length and breadth”);
int x= [Link]( );
int y= [Link]( );
int z= [Link](x,y);
[Link](“Area of rectangle=”+z);
}
}
iv. class Rectangle
{
public int area ( )
{
Scanner sc = new Scanner ([Link]);
[Link](“Enter length & Breadth”);
int l=[Link]( );
int b=[Link]( );
int ar = l*b;
return (ar);
}
public static void main (String args[])
{
Rectangle ob = new Rectangle( );
int z= [Link]( );
[Link](“Area of rectangle=”+z);

}
}

Call by value- This calling is done in parametrised & non-parametrised methods. In case of parameterised
methods the actual values are passed into formal values.

Call by reference- This calling is also done in parametrised methods but here instead of variables, objects
are passed as parameters. Hence, we will study this in class XI & XII.

Actual Parameter Formal Parameter

i. These parameters are used in i. These parameters are used in


method calling. method definition.
ii. Change in values will effect the ii. Change in values will not effect
formal parameters. the actual parameters.

Pure functions-

• Pure functions always return the same output for a given input.
• Pure functions are preferred in software development.

Impure functions-
• Impure functions produce different outputs for the same input.
• Impure functions should be used only when necessary.
Method/Function Overloading-
• Same method name is used with different parameter list.
• Implements the fundamental of Polymorphism.
(Constructor)
Its properties are as follows:
• It is a special type of method.
• It has same name as that of class.
• It doesn’t have a return type, hence it cannot be called.
• It is used for initializing variables and creating objects.
• It is automatically (Implicitly) invoked as soon as the class is created.
• It is also of two types-
i. Parametrised Constructor
ii. Non-Parametrised Constructor (Default Constructor)
• Default constructor uses default values as follows: (int-0, long- 0L, float- 0.0/0.0f, double-0.0/0.0d,
char-‘’, String-“”, boolean-false).
Examples:
i. Using Non-parametrised Constructor
class Shapes
{
int l, b, r, s, a1, a2, a3; Variables given in question are declared.
Shapes( )
{
l=0; Non-parametrised constructor declared with
b=0; default values of all the variables(only required
r=0; for input) present.
s=0;
}
void input( )
First method for accepting values in variables
{
(only for input) required in program using
Scanner class.
}
void calculate( )
{
Second method for calculating things
required in program.

}
void display( )
{ Third method for displaying things required
in program.
}
public static void main(String args[])
{ Creating object with constructor (highlighted
Shapes ob=new Shapes( ); part)
[Link]();
[Link](); Calling (invoking) methods with object.
[Link]();
}
}
ii. Using Parametrised Constructor
class Shapes
{
int l, b, r, s, a1, a2, a3; Variables given in question are declared.
Shapes(int ll, int bb, int rr, int ss)
{
l=ll; Parametrised constructor initialised with
b=bb; parameters values of all the variables(only
r=rr; required for input) present.
s=ss;
} .

** Input related method is generally not available in parametrised constructor-based


questions as all inputs are taken through parameters of constructors.

void calculate( )
{
First method for calculating things required in
program.

}
void display( )
{ Second method for displaying things required
in program.
}
public static void main(String args[])
{
Scanner sc=new Scanner ([Link]);
[Link](“Enter length, breadth, side and radius”);
int len=[Link]();
int bre=[Link]();
int sd=[Link]();
Creating object with parametrised
int rad=[Link]();
constructor (highlighted part)
Shapes ob=new Shapes(len,bre,sd,rad);

[Link](); Calling (invoking) methods with object.


[Link]();
}
}
• If both the constructor (parametrised and non-parametrised) are used in same code (program) then it
is known as constructor overloading.

You might also like