functions in java

Post on 13-Jun-2015

18.139 Views

Category:

Education

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Pure and impure functions

TRANSCRIPT

FUNCTIONS/METHODS IN JAVA

FUNCTION

• A program module used simultaneously at different instances in the program is called method or function

Defining a function• <Access specifier><Return type><method name>(parameter list)• {• //Body of the function• }• Access specifier – public or private. • Method declared without access specifier is by default treated as public• Type- Specifies the data of the value returned from the method• Function name – Preferably related to the program• Parameter list – Variables which receives the value passed from the

arguments during method call

Components of a method

• There are two components• Header ( also known as method prototype)• Body

• List of parameters is called signature

HeaderPublic int add()

Bodyreturn(value)

The statement which sends back the value from a method is caller program

Return statement is used at the end of the program which is a function terminator

Types of function

Function

Pure Function /Accessor method Impure Function /

Mutator method

Pure functionPure Function /

Accessor method

ClassFunction called

FunctionCalculations done

Value returned

A function which returns a value to its caller module

Program for pure function

class add{void sum(int a, int b){int c = calc(a,b);System.out.println(“The sum is” +c);}int calc(int a, int b){Int c=a+b;return(c);}}

Impure functionimpure Function /Accessor method

A function which does not return a value . It will be a void functionChanges the state of the object each time the function is called

ClassFunction called

FunctionCalculations done

Value not returned

Program for pure function

class add{void sum(int a, int b){calc(a,b);}void calc(int a, int b){int c=a+b;System.out.println(“The sum is” +c);}}

Different ways of defining a function

• Receiving value and returning outcome to the caller

• Receiving values but not returning outcome to the caller

• Neither receiving values nor returning outcome to the caller

Formal parameter

• Parameter is a variable used with method signature that receives a value during function call

• When the parameter values are received from the caller then it is called Formal parameter

Actual Parameter

• If the values are passed to the method during its call from the caller then it is actual parameter

Pass by value

• Process of passing a copy of actual arguments to the formal parameters.

• Any change made in the formal will not reflect on actual argument

A B

5 6

X Y

5 6

X Y

7 8

A B

5 6

Actual arguments before function call

Copied formal parameters during function call

Formal parameters after function operation

Actual arguments after function call

PASS BY REFERENCE

• Process of passing the reference of actual arguments to the formal parameters and any change in the formal parameters will be reflected in the actual parameters also

Function overloading

• Function with the same name but used for different purposes is called as overloaded functions

• It is implemented through polymorphism• Compiler finds the best match of the function

arguments and the parameter list during program compilation called static binding

Recursive function

• A function which calls by itself is called as recursive function

top related