functions (methods)

4
Functions (Methods) Rajath R IX A

Upload: sonybabu

Post on 19-Feb-2017

118 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Functions (methods)

Functions (Methods)

Rajath RIX A

Page 2: Functions (methods)

Functions (Methods)What is a function ?

A function is a set of statements that is used to perform a specific task simultaneously at different instances in a program.

Why is it advantageous to use functions ? There are three main reasons why we use functions

in a program, which are : To allow us to cope with complex computational tasks into

a collection of smaller methods so that problem solving becomes easier.

To hide low-level inner code details that otherwise seem obscure and confuse the end-user.

To re-use portions of code as and when necessary by simply using the method names.

Page 3: Functions (methods)

Functions (Methods)How do you define a function ?

A function is defined using the syntax given below. Syntax :<access specifier> <return data type> <function name>

( <parameter list> ){

// block of statements ;return <return value>;

}Give an example of a function.

public double simpleinterest ( int p, int t, int r ){

double sim = ( p * t * r ) / 100 ;return sim ;

}

Body of the function

]

Page 4: Functions (methods)

Functions (Methods)What are the components of a function ?

A function has the following components :