6 Functions in Java

6.1 INTRODUCTION

A function is a self-contained block of code written once for a particular purpose, but can be used again and again. It is a basic entity for Java programming language. Even the execution of the program starts from the main function. It is the basic building block for modular programming.

Depending on the parameters and return type (discussed later on), functions are classified into four categories:

1.   No return type and no arguments/parameters.

2.   No return type but arguments/parameters.

3.   Return type but no argument/parameters.

4.   Return type with argument/parameters.

All the above categories except the third one are discussed in this chapter. The third category is discussed in the chapter titled 'Classes and Object'.

Various methods have been presented so far for different programs. For example, the methods provided by Math library are sqrt, pow, max, etc. All these functions are library functions or built-in functions, i.e., they are already there in the Java programming language and can be used in different programs. Again, some of these functions belong to one of the categories given above. The limitations of these functions are that their code cannot be known; they can simply be used and no modification can be done to them.

All the built-in functions have their prototype or declaration stored in their respective packages. For example, function declaration of sqrt and pow is stored in the package Java.lang. Particular functions can be written depending on the requirements which are called user-defined functions as they are defined by the user and put in any of the categories given above.

The important point to note here is that main() is not a library function. It is simply a restriction from Java compiler that one has to use this function for programming as execution starts from this function. So this can be put into the user-defined function. The discussion in this chapter will entirely be based on the user-defined functions.

6.2 FUNCTION DECLARATION AND DEFINITION

In Java, each function that is used must be declared and defined first. The declaration of the function is also termed as 'prototyping'. A function prototyping tells the compiler three things about a function: function name, return type of the function, and number of arguments.

For example, a function sum which takes two arguments of type int and return an int can be written as follows:

images

The above declaration tells the compiler that sum is a function which accepts two parameters of type int and returns type of the function int. In Java, as function has to be defined at the place, its declaration and definiton (discussed below) cannot be separated. The basics of functions can be understood using a small example.

class JPS
{
      static void show()
      {
             System.out.println("Hello from show");
      }
      public static void main(String[] args)
      {
             show();
      }
}

The line static void show () is the function name or prototype of the function. The general syntax is as follows:

void function_name(){ }

In the above context, void is the return type, show is the function name and () after function show indicates that this show function does not take any arguments. Whenever there is () after any name then that is a function. Obviously, you may have arguments within (). Here, void means function, which neither takes any argument nor returns any value. The static keyword is a must as objects are not being made use of here. As main is the static function and all coding can only be executed from the main, it can be called show from main. Now main being a static function, only other functions which are static can be called main.

The actual working of the function is done by the definition of the function which is given between the pair of braces.

{
System.out.println("Hello from show");
}

All the statements within {} of the function is called the body of the function. When show() is written, it means the function is called show(). Whenever show() statement is encountered, the control is transferred to the body of the function and all the statements written within it gets executed. When closing brace of the function is encountered, the function is returned to the next statement from where the function was called. In Java, every function will be called from some other function. Here, show() has been called within main(). So, main() is called the calling function and show() is called the called/ callee function.

/*PROG 6.1 WORKING WITH TWO FUNCTIONS */
 class JPS1{
     static void show()
     {
            System.out.println("Hello from show");
     }
     static void disp()
     {
            System.out.println("Hello from disp");
     }
     public static void main(String[] args)
     {
            System.out.println("In main");
            show();
            disp();
            System.out.println("Back in main");
     }
 }
 OUTPUT:

 In main
 Hello from show
 Hello from disp
 Back in main

Explanation: Here, we are working with two functions. As is clear from the output, initially first System. out.println in main() is executed and then show() is called and System.out.println within show is executed. When show() returns disp() is called and System.out.println within disp() is executed. When disp() is returned, the second System.out.println in main() is executed and at the end, the program terminates.

6.3 NO RETURN TYPE BUT ARGUMENTS

The general syntax of this category of function is as follows:

void function_name(data_type args,..........)
{
      Statements;
      Statements;
      .......... ;
      .......... ;
}

The parameters are passed to function when the function is called; they are passed from calling function to the called function. In called function context, the parameters are known as formal parameters and in calling function context, they are known as actual parameters.

/*PROG 6.2 DISPLAY OF INTEGER THROUGH FUNCTION VER 2 */
import java.io.*;
import java.util.*;
class JPS2
{
   static void show(int x)
   {
          System.out.println("You entered := " + (x++));
   }
   public static void main(String[] args)
   {
          Scanner sc = new Scanner(System.in);
          int a;
          System.out.print("

Enter a number :=");
          a = sc.nextInt();
          show(a);
          System.out.println("After function a := " + a);
   }
}
OUTPUT:

Enter a number     :=123
You entered        :=123
After function a   :=123

Explanation: The definition of the function void show(int) states that function show does not return any value but accepts an argument/parameters of int type, i.e., int variable or an int constant will be passed to this function. When the function is called, an int value is passed as shown by the statement show(a). Here, the function is called show and passing 'a' as an argument. This is known as call by value as the function and passing value of the variable are called 'a'. The 'a' value sent must be collected in some variable in the function definition. This value is collected in variable 'x'. Note that the type and number of arguments must match when defining the function. Variable 'a' in the function main is called the actual argument and the variable 'x' in the function show is called the formal argument. When 'a' is passed to the function show a copy of 'a' is sent which is collected in 'x'. In the function, the value 'x' is simply printed. The change in 'x' in function show does not affect the original value of 'a'.

Note: All primitive data constant or variables are always passed by value and object is always passed by reference.

/*PROG 6.3 PRINT FIBONACCI SERIES USING FUNCTION */
import java.io.*;
import java.util.*;
class JPS5
{
  public static void main(String[] args)
  {
         int num;
         Scanner sc = new Scanner(System.in);
         System.out.println("
Enter an integer");
         num = sc.nextInt();
         System.out.println("
Fibonacci series is
");
         fibbo(num);
  }
  static void fibbo(int n)
  {
         int i, a = 0, b = 1, c;
         System.out.print(" " + a);
         System.out.print(" " + b);
         for (i = 1; i <= n - 2; i++)
         {
                c = a + b;
                System.out.print(" " + c);
                a = b;
                b = c;
         }
  }
}
OUTPUT:

Enter an integer
9
Fibonacci series is
0 1 1 2 3 5 8 13 21

Explanation: The Fibonacci series is the series where each term of the series is given by the following formula:

F(n)=F(n-1)+F(n-2) for n >=2

That is, each term of the series is the sum of its previous two terms. The first two terms are assumed to be 0 and 1.

The number of terms to be displayed is taken in the variable num from the user. This is passed into the function fibbo. The first two terms are displayed as it. The rest of the terms are calculated within the loop. For each new term the sum of the two previous terms are first found, e.g., a and b into c. The value of b into a is then assigned and the new calculated value of c into b. The loop runs for n - 2 times as the first two values 0 and 1 are displayed outside the loop.

/*PROG 6.4 DISPLAY DECORATED NAME USING FUNCTION */
import java.io.*;
import java.util.*;
class JPS6
{
   public static void main(String[] args)
   {
          String name;
          Scanner sc = new Scanner(System.in);
          System.out.println("

Enter your name");
          name = sc.next();
          line(25, '+'),
          System.out.println("

		Welcome " + name);
          line(25, '*'),
          System.out.println("

");
      }
      static void line(int n, char p)
      {
          int i;
          System.out.println();
          for (i = 1; i <= n; i++)
                 System.out.print(" " + p);
      }
}
OUTPUT:

Enter your name
Hari
+ + + + + + + + + + + + + + + + + + + + + + + + +
             Welcome Hari
* * * * * * * * * * * * * * * * * * * * * * * * **

Explanation: The function line accepts two parameters; the first one is an integer and the second is a character. The function prints the second argument, the first number of times, i.e., in the above program when function line (25, ' +') is called, it first leaves a blank line and then using for loop prints ' + '25 times. Depending on the requirement, any value for the parameters may be put.

6.3.1 Returning Prematurely from Function

When the function is called, the code contained within the function is executed and closing braces of the function is encountered and the function execution comes to end. This is the normal flow of execution during function call. In some cases, when we want to return from function early without completing the whole of its execution, we may use return statement. This may be on the basis of certain decision making statements like if or some loop. To come out early from a method, we can use return statement in the following way:

void demo()
{
      statements;
      statements;
      statements;
      if(condition)
      return;
      statements;
      statements;
}

An example is given below.

/*PROG 6.5 RETURNING PREMATURELY FROM FUNCTION */
class JPS7
{
   static void demo(int x)
   {
          if (x < 0)
          {
                System.out.println("No negative value");
                return;
          }
          System.out.println("

Hello from demo
");
   }
   public static void main(String[] args)
   {
          demo(30);
          demo(-30);
   }
 }
 OUTPUT:

 Hello from demo
 No negative value

Explanation: In the method 'demo' when a negative value is passed in the function, it returns prematurely by printing "No negative value" otherwise, it prints "Hello from demo" and returns maturely.

6.4 FUNCTION WITH PARAMETERS AND RETURN TYPE

The general syntax of this category of function is as follows:

return_type function_name(data_type arg,..........)
{
      statements;
      statements;
      ..........;
      return data;
}

where type of data must be return_type.

The parameters are passed on to the function. Inside the function, any processing or computation may be carried out onto the formal parameters along with some local variables, but in the end, the function must return a value of type return_type. That value replaces this function call in the callee function.

/*PROG 6.6 COMPUTE SQUARE OF FUNCTION AND RETURN USING FUNCTION */
import java.io.*;
import java.util.*;
class JPS8
{
        public static void main(String[] args)
      {
             Scanner sc=new Scanner(System.in);
             double num, s;
             System.out.println("
Enter a number");
             num=sc.nextDouble();
             s=sqr(num);
             System.out.println("
Square of "+num+" is "+s);
      }
      static double sqr(double x)
      {
             double t;
             t = x * x;
             return t;
      }
}
OUTPUT:

Enter a number
12
Square of 12.0 is 144.0

Explanation: The function double sqr(int) tells the compiler that sqr is a function which accepts an argument of type double, and the double before function name sqr represents return type of function sqr, i.e., a double value will be returned from function sqr through return statement. In the definition/ body of the function, x*x is calculated into t and the value of t is returned through statement return t. When this happens, the t returns at the place from where the function sqr was called, so the statement s = sqr (num) becomes s = t. If the value of x happens to be 12, t will have 144.0 when it returns and the same will be assigned to t.

Alternative function definition may be written as:

double sqr(double x)
{
   return x*x;
}
/*PROG 6.7 FINDING THE FACTORIAL OF A NUMBER */
import java.io.*;
import java.util.*;
class JPS10
{
   public static void main(String[] args)
   {
          Scanner sc = new Scanner(System.in);
          int num, s;
          System.out.println("
Enter a number");
          num = sc.nextInt();
          s = fact(num);
          System.out.println("Factorial of "+num+" is "+s);
   }
   static int fact(int x)
   {
          int f = 1, i;
          for (i = 1; i <= x; i++)
          f = f * i;
          return f;
   }
}
OUTPUT:

Enter a number
7
Factorial of 7 is 5040

Explanation: The factorial of a number x is the product of 1*2*3*4....x. For example, factorial of 4 is 1*2*3*4, i.e., 24. The number whose factorial is to be found is passed into the function fact and is collected into x. We take a variable f into which factorial of x will be stored. This is initialized to 1. In the function fact, a for loop is run from 1 to x. We show the steps for value of x = 4.

Step1          i=1          f = f * i        = 1 * 1 = 1
Step2          i=2          f = f * i        = 1 * 2 = 2
Step3          i=3          f = f * i        = 2 * 3 = 6
Step4          i=4          f = f * i        = 6 * 4 = 24
/* PROG 6.8 MAXIMUM OF THREE NUMBERS FROM A FUNCTION WHICH FIND MAXIMUM OF TWO NUMBERS */
import java.io.*;
import java.util.*;
class JPS11
{
   public static void main(String[] args)
   {
          int m, a, b, c;
          Scanner sc = new Scanner(System.in);
          System.out.println("
Enter the three numbers 
");
          a = sc.nextInt();
          b = sc.nextInt();
          c = sc.nextInt();
          m = max(a, max(b, c));
          System.out.println("
a= "+a+"	b= "+b+"	c= "+c);
          System.out.println("
Maximum of three numbers:=   "+m);
   }
   static int max(int x, int y)
   {
          return x > y ? x : y;
   }
}
OUTPUT:

Enter the three numbers
123 456 789
a= 123 b= 456 c= 789
Maximum of three numbers: = 789

Explanation: In the expression m = max (a, max (b, c)) the first max (b, c) is called and when this returns, the call max (b, c) is replaced by the maximum of two, either b or c, say t (assume). Then again, function max is called which returns max of a and t. Note how the maximum of three numbers is calculated with a function which finds maximum of two numbers only.

On the similar basis, max of 4 numbers can be calculated by writing the following:

m = max (max (a, b), max (c, d));

6.5 RECURSION

Recursion is a programming technique in which a function calls itself for a number of times until a particular condition is satisfied. It is very important to understand and once understood many long listing of code can be reduced to a few number of lines. Recursion is basically a word mostly used in mathematics to state a new term within previous term, such as the following:

X n+1 = X n +1 for n>=1 and x 1 = 1

Here, X2 can be calculated in terms of X1, X3 in terms of X2 and so on.

When solving a problem through recursion, two conditions must be satisfied:

1.   The problem must be expressed in recursive manner.

2.   There must be a condition which stops the recursion, otherwise there would be a stack overflow.

/* PROG 6.9 DEMO OF RECURSION */
class JPS12
{
   static int t;
   public static void main(String[] args)
   {
          if (t == 5)
          {
                System.out.println("

Quit");
                System.exit(0);
          }
          System.out.println("
 Hello from main" + (++t));
          main(args);
   }
}

images

Figure 6.1 Output screen of Program 6.9

Explanation: The t is a static variable. It retains its previous value even after the function execution is complete. When main() executes for the first time, the value of t is 0. If the condition fails and System.out.println after if executes. When control reaches at main(args) the recursion starts as we are calling main from the main. The main starts again for this call with the value of t = 1. Note that static int t; is written inside the class but not in the main function. For value t=1, System.out. println after if gets executed and main (args) is called again and this time with the value of t=2. This continues until t does not become 5. When t becomes 5 and if the condition satisfies, the recursion stops and the program terminates.

If we do not take 't' as a static variable in the class but not in the function, then first of all, call program will give compilation error as main is static function and inside static function only other static function and static variable can be used. Second, if we try to declare t inside the function without a static qualifier, the infinite loop will result as when recursion starts (t will have to be initialized with 0), the previous value of t will not be retained and instead, each time the main will be called and t will be initialized with 0.

/* PROG 6.10 FACTORIAL OF A NUMBER USING RECURSION */
import java.io.*;
import java.util.*;
class JPS13
{
   public static void main(String[] args)
   {
          int ans, num;
          Scanner sc = new Scanner(System.in);
          System.out.println("
Enter an integer");
          num = sc.nextInt();
          ans = fact(num);
          System.out.println("Factorial is " + ans);
   }
   static int fact(int n)
   {
          return (n < 1 ? 1 : n * fact(n - 1));
   }
}
OUTPUT:

Enter an integer
5
Factorial is 120

Explanation: Assume n is 4, now recursion works as follows:

N     Function name
4     4 * fact (3)
3     4 * (3 * fact (2))
2     4 * (3 * (2 * fact(1)
1     4 * (3 * (2 * 1))

When recursion starts, each call to function fact creates a new set of variables here only one. Whenever recursion starts, the recursive function calls do not execute immediately (in reality function addresses are put). They are saved inside the stack along with the value of variables. (A stack is a data structure which grows upward from max_limit to 1. Each new item 'pushed' in the stack takes its place above previously entered item. The items are 'popped' out in the reverse order in which they were entered, i.e., the last item is popped out first. This being the main reason that they are called LIFO (last in first out). This process is called winding in the recursion context. When recursion is stopped in the above program and when n becomes 1 and the function returns the value 1, all the function calls saved inside the stack are popped out from the stack in the reverse order and get executed, i.e., fact (1) returns to fact(2), fact(2) return to fact(3) and at the end, fact(3) returns to fact(4) which ultimately returns to the main. This process is called unwinding.

6.6 FUNCTION OVERLOADING

Overloading refers to the use of same function for different purposes. Function overloading refers to creating number of functions with the same name which performs different tasks. Function overloading is also known as function polymorphism. Function overloading relieves us from remembering so many function names with the type of arguments they take. In function overloading, we can create a number of functions with the same name but the number of arguments or type of arguments must be different. A few examples are as follows:

1.    int sum(int);
float sum(float);
double sum(double);

In this example, three functions are declared with the same name sum. Each function takes just one parameter but all are of different types, i.e., the number of parameters in all overloaded function sum is the same but the type of parameter is different.

2.    void show(int, int);
void show(int);
void show(int, int, int);

In this example, the number of parameters is not the same in the show functions but the type is the same.

3.    void show(int, char);
void show(char, int, float);
void show(int);
int show(int, int);
float show(char, char, char);

In this example, there is a mix of overloaded show functions. Some of them have same number of arguments but type is different, while some others have same type of arguments but numbers of arguments are different.

When there are a number of overloaded functions in a program, in which function to call is determined by either checking type of argument or number of argument, note that the return type does not play any role in function overloading, because the which function to call is determined by checking the type and number of argument a function accepts. When control is transferred to function and function is about to return after execution, return type comes to play.

In function overloading the compiler first tries to find an exact match. If the exact match is not found the integral promotion is used. The user-defined conversion methods may also be used in case a class object is to be converted to any built-in type or vice versa.

/* PROG 6.11 DEMO OF FUNCTION OVERLOADING ver 1*/
import java.io.*;
import java.util.*;
class JPS15
{
   public static void main(String[]args)
   {
          show('h'),
          show(15);
          show(34.67f);
          show("NMIMS University");
   }
   static void show(int x)
   {
          System.out.println("int x                  := "+x);
   }
   static void show(float x)
   {
          System.out.println("float x               := "+x);
   }
   static void show(char x)
   {
          System.out.println("char x                 := "+x);
   }
   static void show(String x)
   {
          System.out.println("String x               := "+x);
   }
}
OUTPUT

char x    := h
int x     := 15
float x  := 34.67
String x  := NMIMS University

Explanation: In the program, the function show is overloaded four times. The function takes a single parameter but each parameter is different in each function as can be seen in the program. In the main function, show is called with four literals of type int, char, float and String. Each parameter is passed to show and show is called four times. The compiler depending on the type of argument to function show calls the respective version of the show function, i.e., in case of show(15), the show function of int version will be called and so on.

/*PROG 6.12 DEMO OF FUNCTION OVERLOADING VER 2, IMPLICIT PROMOTION */
import java.io.*;
import java.util.*;
class JPS16
{
   public static void main(String[] args)
   {
          show(25);
          show('h'),
          show(2.5f);
          show(4.56);
   }
   static void show(int x)
   {
          System.out.println("
int x         := " + x);
   }
   static void show(double x)
   {
          System.out.println("
double x      := " + x);
   }
}

OUTPUT:

int x        := 25
int x        := 104
double x     := 2.5
double x     := 4.56

Explanation: In the program, the function show is overloaded which takes a single parameter of int and double. In the function call show(25), int version of show is called as 25 is an integer. In function call show('h'), int version of show is called why? The compiler searches for an overloaded show function that takes a parameter of type char, but fails as there is no such overloaded function written. So it does an integral promotion from char to an integer and calls the int version of show function displaying ASCII value of 'h'. Similarly, in function call show(2.5f), the compiler looks for an overloaded function show which takes float type argument and if fails the overloaded function show with double type value is called after doing promotion of float to doubles.

 /* PROG 6.13 FUNCTION OVERLOADING MAX OF TWO NUMBERS */
import java.io.*;
import java.util.*;
class JPS19
{
   public static void main(String[] args)
   {
          int x, y, intmax;
          float p, q, fmax;
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter two integers");
          x = sc.nextInt(); y = sc.nextInt();
          System.out.println("Enter the two floats");
          p = sc.nextFloat(); q = sc.nextFloat();
          intmax = max2(x, y);
          fmax = max2(p, q);
          System.out.println("Max of two int is:="+intmax);
          System.out.println("Max of two float is:="+fmax);
   }
   static int max2(int x, int y)
   {
          return (x > y ? x : y);
   }
   static float max2(float x, float y)
   {
   return (x > y ? x : y);
   }
}

OUTPUT:

Enter two integers
12 56
Enter the two floats
12.34
56.78
Max of two int is             := 56
Max of two float is          := 56.78

Explanation: In the program, there are two overloaded version of function max2, which takes two parameters of type int and float and returns the maximum of two numbers. Depending on the type of parameter, the appropriate version of max2 function is called.

6.7 PONDERABLE POINTS

1.   Based on the nature of creation there are two categories of functions: built-in and user defined.

2.   The functions that are predefined and supplied along with the compiler are known as built-in functions.

3.   The function main() can appear only once in a Java program, because execution commences from the first statement in the main() function. If there is more than one main() function, there will be a confusion while commencing the execution.

4.   A function that perform no action is known as a dummy function. It is a valid function. Dummy function may be used as a place-holder, which facilitates adding new functions later on. For example: void dummy(){}

5.   Advantages of recursion include the following:

(a) Easy understanding of the program logic.

(b) Helpful in implementing recursively defined data structure.

(c) Compact code can be written.

6.   Use of return statement helps in early exiting from a function apart from returning a value from it.

7.   Function prototyping, declaration or signature are all the same thing.

8.   Function declaration tells three things to the compiler:

(a) Function name

(b) Type and number of argument it takes

(c) Return type of the function

9.   Function overloading is the creation of a number of functions with the same name but differing either in type of argument or number of arguments.

10. There is no limit on the number of functions that can be overloaded in a program.

11. Return type does not serve any purpose in function overloading.

REVIEW QUESTIONS

1.   Write the definition of a function. Indicate the types of functions available in Java.

2.   Differentiate between library and the user defined function.

3.   How does a function work? Explain how arguments are passed and results are returned.

4.   What are actual and formal arguments? Explain with suitable examples.

5.   What are void functions?

6.   Why is return statement not necessary when function is called by reference?

7.   What is recursion? Explain its advantages.

8.   Explain the types of recursion.

9.   Is it possible to call library function recursively?

10. Does the function prototype match with function definition?

11. What is the main () in Java? Why is it necessary in each program?

12. Write a program to compute the total amount payable with annual interest for a given number of years. The inputs are principal amount, rate of interest and number of years. Attempt the program with or without recursion.

13. Write a program to count for how many times a function called. Create a user defined function.

14. Write a Java program using a function roots to calculate and display all roots of the quadratic AX2 + BX + C = 0.

15. Write a Java program which displays word equivalent of a number of only 3 digits, that is, 123 should be displayed as one hundred twenty-three.

16. Write a function that reads a number in binary form and converts it into hexadecimal form. Hexadecimal values must be stored also.

17. Write a program to calculate average temperature of five days. Create temp () function.

18. Write a program to add return value of three functions.

19. Consider the following recursive method JPS

private static int JPS(int n)
{
   if(n == 0)
   return 1;
   return 3*JPS(n-1)+1;
}

Which corresponds to the recursive series:

JPS(0) = 1
JPS(n) = 3.JPS(n-1)+1

Write a driver that prints out the first 10 values of the series. What is the closed form of JPS?

20. Consider the Fibonacci function:

Fib (0) = 1
Fib (1) = 1
Fib (n) = Fib (n-2) + Fib (n -1)

Use hand-tracing to compute the following values:

a. Fib(3)

b. Fib(4)

c. Fib(5)

Notice how much extra work is required to compute these values because we need to compute the same value, for example, Fib (2) many times.

21. Using the recursive definition of the Fibonacci sequence, what is the highest term that your computer system can comfortably compute? Use the long data type so that you can avoid integer wraparound as long as possible.

22. Consider the following method:

public static int JPS(int n)
{
   if(n==0)return 0;
   return n + JPS(n-1);
}

What does this method compute?

Multiple Choice Questions

1.   How many main() function can be defined in a Java program?

(a) 1

(b) 2

(c) 3

(d) Any number of times

2.   A function with no action

(a) is an invalid function

(b) produces syntax error

(c) is allowed and is known as dummy function

(d) none of the above

3.   The default return data type in function definition is

(a) int

(b) char

(c) float

(d) double

4.   The parameters in function definition are

(a) actual parameters

(b) formal parameters

(c) dummy parameters

(d) optional

5.   Recursive calls result when

(a) a function calls itself

(b) a function calls another function, which in turn calls the fucntion1

(c) both (a) and (b) are correct

(d) a function calls another function

6.   Functions have

(a) file scope

(c) block scope

(b) local scope

(d) function scope

7.   When a function is recursively called all automatic variables are

(a) storedinstack

(c) stored in array

(b) storedinqueue

(d) stored in linked list

8.   Parameters are used

(a) to return values from the called function

(b) to send values from the calling function

(c) both (a) and (b)

(d) to specify the data type of the return value

9.   A function is identified by an open parenthesis following

(a) a keyword

(b) an identifier other than keyword

(c) an identifier including keyword

(d) an operator

10. Function overloading is the creation of function with same name but they differ

(a) according to their return type

(b) in type of argument

(c) in the number of argument

(d) either in type of argument or number of arguments

11. Which of the statements is correct for function overloading?

(a) return type does not serve any purpose

(b) type of argument should be equal for the function

(c) number of argument should be the same

(d) none of the above

12. A function that performs no action is called as

(a) passive function

(b) recursive function

(c) dummy function

(d) none of the above

13. In a Java program one can overload

(a) 1 function

(b) 2 functions

(c) 5 functions

(d) any number of functions/no limit

14. The function void dummy(){} is

(a) invalid function

(b) valid function

(c) performs no action

(d) both (b) and (c)

15. In Java all primitive data constants or variables are always passed

(a) by value

(b) by reference

(c) by using dummy function

(d) none of the above

KEY FOR MULTIPLE CHOICE QUESTIONS

1.   a

2.   c

3.   a

4.   b

5.   c

6.   a

7.   a

8.   b

9.   b

10. d

11. a

12. c

13. d

14. d

15. a

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.163.180