Methods/Functions in Java


☞A program segment that contains executable instructions within { }, to perform a specific task is known as Method or Function.

☞It exist as a members of a class.

☞Syntax : <access_specifier> <return_type> <function_name><(Parameter List)>{....}

☞Example : public int sum(int a, int b) {……}

public  ➺    access_specifier
int     ➺    return_type
sum     ➺    function_name
a,b     ➺    parameter_list

☞public int sum(int a, int b) is known as function_header and {……} is known as funtion_block.


Advantages of Using a Method

☞It is used to reuse the segment of code, as and when necessary.

☞It occupies less memory space and executes faster.

☞It divides a complex computational task into a collection of smaller methods.


Note :
1. Method header and method prototype is the same term.
2. Method signature is written by using the method name along with the parameters to be passed to the method. Example : sum(a,b)
3. Access-specifier can be either public, protected or private. It deals with the scope of usage of fuction.