functions in java

19
FUNCTIONS/METHODS IN JAVA

Upload: sonybabu

Post on 13-Jun-2015

18.139 views

Category:

Education


1 download

DESCRIPTION

Pure and impure functions

TRANSCRIPT

Page 1: Functions in Java

FUNCTIONS/METHODS IN JAVA

Page 2: Functions in Java

FUNCTION

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

Page 3: Functions in Java

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

Page 4: Functions in Java

Components of a method

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

• List of parameters is called signature

Page 5: Functions in Java

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

Page 6: Functions in Java

Types of function

Function

Pure Function /Accessor method Impure Function /

Mutator method

Page 7: Functions in Java

Pure functionPure Function /

Accessor method

ClassFunction called

FunctionCalculations done

Value returned

A function which returns a value to its caller module

Page 8: Functions in Java

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);}}

Page 9: Functions in Java

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

Page 10: Functions in Java

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);}}

Page 11: Functions in Java

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

Page 12: Functions in Java

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

Page 13: Functions in Java

Actual Parameter

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

Page 14: Functions in Java

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

Page 15: Functions in Java

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

Page 16: Functions in Java

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

Page 17: Functions in Java

Function overloading

Page 18: Functions in Java

• 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

Page 19: Functions in Java

Recursive function

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