37 Points to Master Functions or Methods in Java


Java / Tuesday, July 30th, 2019

Following are the 37 points to Master Functions or Methods in Java.

Functions or Methods

A Java class is a group of values with a set of operations. The user of a class manipulates object of that class only through the methods of that class.

General form of a method is:

type name_of_method (argument_list)
{
// body of method
}

 37 Points to Master Functions or Methods in Java

1. A function, also called a method, is a block that contains executable instructions within { }, to perform a specific task.

2. A function returns a value which may be used for further computation.

3. Functions exist as members of a class.

4. The purpose of using a function is (i) to hide implementation details, (ii) to use codes, and (iii) to handle complex situations in a problem.

5. A function may be classified as (i) pure or (ii) impure.

6. A pure function is one which may have a return value; but cannot change the state of the parameters. It is also known as accessor.

7. A pure function can receive values for the parameters of an object, may have a return value, but cannot change the state of the parameters of the object.

8. A function which can return a value and also may change the state of an object is called an impure function or updater or mutator.

9. Impure functions are used to reset the values of the parameters.

10. A function may be defined anywhere in a program.

11. A function is to be defined before it is used.

12. A function name must be a valid identifier and meaningful.

13. In usual practice, a function is defined in the following format:

<access-specifier> <modifier> <return type> <function-name> <(parameter list)>

Body of a Function:
{
.
.
.
}

14. The access specifier may be public, private, protected or friendly (by default).

15. The modifier may be static, final etc.

16. Return type may be any valid type of value. It is ‘void’ type when no value is to be returned.

17. The parameter list is enclosed within parentheses ( ). It indicates the number, symbols and datatypes of the parameters.

18. Argument and parameter are loosely used for each other. But there is a slight difference between them. An argument is a value of a specific datatype. The value is passed to the parameter which represents it. This is done during program execution.

19. During execution of a function, the value of the argument is referenced by the respective parameter symbol.

20. During method invocation, arguments are to be passed matching the number, datatype and order of the parameters in the list of the method.

21. If datatypes of the arguments are not same, they must at least be convertible to the defined type in the parameter list, by typecasting.

22. During processing, specific values are provided to the parameters.

23. For substituting definite values to the parameters, the function must be called appropriately.

24. Before calling the function, variables in the form of parameters are required to be initialized.

25. Every function is said to have a signature, which refers to the number and datatypes of the parameters.

26. Values are passed to the function through the arguments.

27. Primitive type values are ‘passed by value’.

28. Reference types, i.e., objects, arrays are ‘passed by reference’.

29. Thus, ‘call by value’ and ‘call by reference’ are the two ways of calling functions.

30. A function prototype is the first introduction of a function definition with

  • Return type
  • Number of parameters, and
  • The type of each parameter.

31. Function signature is a part of the function prototype.

32. In the pass-by-value (also known as call-by-value), a working copy of the called function prototype is created with variable names different from the parameter list. The values actually transmitted are substituted in these variables.

33. In pass-by-reference (also known as call-by-reference), the reference of the passed variable, an array, or object, whatsoever may be is transmitted to the actual original copy of the function prototype.

34. The value of the function is returned through the return statement.

35. The function which does not return any value is referred as a ‘void’ function, which has ‘void’ as its return type.

36. When in a class, the same function name is used in defining more than one function (with same scope) which are distinct and distinguishable either by number or types of parameters, the process is said to be function overloading.

37. The package that provides mathematical functions is the Math class. For using this, the java.lang.Math class should be included (imported) in a program.

 

 Example Program

Suppose we define a class to represent complex numbers. The complex class definition shown in the program given below illustrates how this can be done. Two variables real and imag, are declared. These represent the real and imaginary parts of a complex number (respectively). The program also defines three methods, assignReal and assign Imag() and showComplexl()that can be used to assign values to the real is () part imaginary part of a complex number, and to show the real number respectively.

Output of this program is:

The Complex Number is :5.0+i2.0

 Question 01

Differentiate between call by value or pass by value and call by reference or pass by reference.

Call/Pass by value

Call/ Pass by reference

Parameters are copies of actual parameters. Parameters contain the reference to the actual parameters.
Any change in formal parameter is not reflected in the actual parameter. Change in the formal parameter is automatically reflected back in the corresponding actual parameter.
Primitive datatypes are passed in this way. Reference

 

 Question 02

Give difference between a constructor and a method.

The answer is already discussed in the post 20 Facts or Points to Master Constructor in Java..

 Example Program

Write a program to find current date and time.

 

Leave a Reply

Your email address will not be published. Required fields are marked *