Anda di halaman 1dari 3

FUNCTIONS

A function is named unit of a group of program statements that carry out a particular
task. This unit can be invoked/called from other parts of the program.
Reasons to use a method/function:
o To allow us to cope with complex problems
When programs become more and more complex i.e. when they grow
in size, generally they become unruly. So a complex task is taken and
divided into smaller, more easily understood tasks. In Java those smaller
tasks are implemented as functions.

o To hide the low-level details that otherwise obscure and confuse


Once a method is defined, it can be used in a program. When we are
using it, we neednt concern ourselves with how the method gets the
task done.

o To reuse the portions of code


Once a task is packaged in a method, that method is available to be
accessed, or called, from anywhere in a program. The method can be
reused. It can be called more than once in a program, and it can be called
from other programs.

General form of methods:

[access-specifier][access-modifier] return-type method-name(parameter list)


{
body of the method
}

Conventions used for method name:


1. It should be meaningful.
2. It should begin with a lowercase letter
3. The method names should, generally, begin with a verb followed by one or
more nouns.
Parameter List: It is a comma-separated list of variables of a method referred to
as its arguments or parameters.
Functions are of two types: (1) Built-In Functions (2) User-Defined Functions
Prototype: The first line of the method definition that tells the program about the type
of the value returned by the method and the number and type of arguments.
Function Signature: It is a part of the method prototype that basically refers to the
number and type of arguments.
Void: Void datatype specifies an empty set of values and it is used as the return type
for methods/functions that do not return a value.
The parameters that appear in the method definition are called formal parameters.
The parameters that appear in method call statement are called actual parameters.
PASS/CALL BY VALUE (WHEN PRIMITIVE DATATYPES ARE ALTERED)
The call by value method copies the values of the actual parameters into the formal
parameters, that is, the method creates its own copy of argument values and then uses
them. Whatever changes take place, are not reflected in the main function.
PASS/CALL BY REFERENCE (WHEN REFERENCE DATATYPES ARE
ALTERED)
In the call by reference method, the called method does not create its own copy of the
original values, rather, it refers to the original values only by different names.
Whatever changes take place, are reflected in the main function.
Uses of RETURN Statement:
An immediate exit from the function is caused as soon as a return statement is
encountered and the control passes back to the main function.
The return statement is used to return a value to the calling code.
Types of User-Defined Functions
1. Computational Functions: Functions that return some value.
2. Manipulative Functions: Functions that manipulate information and return true
or false.
3. Procedural Functions: The functions that perform an action and have no
explicit return value.
Methods are also classified into Pure and Impure Methods.
PURE METHOD: A pure method is the one that takes objects or primitives as
arguments but does not modify the objects. The return value of a pure method is
either a primitive or a new object created inside the method.
IMPURE METHOD: An impure method takes objects and changes/modifies the
state of received object.
A function name having several definitions in the same scope that are
differentiable by the number or types of their arguments, is said to be an
overloaded function. The process of creating overloaded functions is
called function overloading.

Anda mungkin juga menyukai