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

3 User Defined Methods

Uploaded by

Archana Gaur
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)
7 views

3 User Defined Methods

Uploaded by

Archana Gaur
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
You are on page 1/ 2

User-defined Methods C HAP T ER :3

FUNCTION A FUNCTION IS INVOKED


Function can be defined as a named unit of a
group of program statements. This unit can be
IN TWO MANNERS
invoked from other parts of the program. Call by value and call by reference, these are also
called as pass by value and pass by reference.

FUNCTION DEFINITION
The general form of a function definition is as PASS BY VALUE
given below : The pass by value method copies the values of
[access-specifier] [modifier] return-type func- actual parameters into the formal parameters,
tion-name (parameter list) i.e., the function makes its own copy of argument
{ values and use them, thus original argument values
body of the function does not changes. In Java all primitive data types
} are passed by value.

FUNCTION PROTOTYPE PASS BY REFERENCE


A function prototype is the first line of the function In this method a reference of arguments are
definition that tells the program about the type of passed to function parameters, no another
the value returned by the function and the number copy of arguments is created by function. So the
and type of arguments to be passed into function. changes made on parameters of function, will
directly affect the original arguments passed. In
java all objects, arrays and other derived data
FUNCTION SIGNATURE types are passed by reference method.
A function signature basically refers to the number
and types of arguments. Function signature and
return type makes a function prototype.
RETURNING FROM A FUNCTION
Returning from a function not only terminates the
FORMAL PARAMETERS function’s execution but also passes the control
The parameters that appear in function definition back to the calling function. Generally, a return
are called formal parameters. statement is used to terminate a function whether
or not it returns a value. The return statement is
useful in two ways :
ACTUAL PARAMETERS • An immediate exit from the function is caused as
The parameters that appear in function call soon as a return statement is encountered and
statement are called actual parameters. the control passes back to the function caller.
• It is used to return a value to the calling function.
ARGUMENTS TO FUNCTIONS
Arguments to function can be of the following types:
• Primitive data types i.e., char, byte, short, int,
long, float, double boolean
• Reference data types i.e., objects or arrays.

Get your FREE Practice Paper for this Chapter!


Sign up to oswal.io now Click Here
PURE AND IMPURE FUNCTIONS FUNCTION OVERLOADING
A pure function is the one that takes objects A function name having several definitions in the
and/or primitives as arguments but does not same scope that are differentiable by the number
modify the objects. The return value of a pure or types of their arguments, is said to be an
function is either a primitive or a new object overloaded function. Function overloading not only
created inside the method. An impure function implements polymorphism but also reduces number
changes/modifies the state of a received object. of comparisons in a program and thereby makes
the program run faster.

Get your FREE Practice Paper for this Chapter!


Sign up to oswal.io now Click Here

You might also like