Chapter 4: Review Questions

4.1

State whether the following statements are TRUE or FALSE.

  • (a) A function argument is a value returned by the function to the calling program.
  • (b) When arguments are passed by value, the function works with the original arguments in the calling program.
  • (c) When a function returns a value, the entire function call can be assigned to a variable.
  • (d) A function can return a value by reference.
  • (e) When an argument is passed by reference, a temporary variable is created in the calling program to hold the argument value.
  • (f) It is not necessary to specify the variable name in the function prototype.
Answer
  • (a) FALSE
  • (b) FALSE
  • (c) TRUE
  • (d) TRUE
  • (e) FALSE
  • (f) TRUE
4.2

What are the advantages of function prototypes in C++?

Answer

Function prototyping is one of the major improvements added to C++ functions. The prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the type of return values.

4.3

Describe the different styles of writing prototypes.

Answer

General form of function prototyping :
return_type function_name (argument_list)

Example :
int do_something (void);
float area (float a, float b);
float area (float, float);

4.4

Find errors, if any, in the following function prototypes.

  • (a) float average(x,y);
  • (b) int mul(int a,b);
  • (c) int display(….);
  • (d) void Vect(int? &V, int & size);
  • (e) void print(float data[], size = 201);
Answer
No. Error Correction
(a) Undefined symbol x, y float average (float x, float y)
(b) Undefined symbol b int mul (int a, int b);
(c) No error
(d) invalid character in variable name void vect (int &v, int &size);
(e) Undefined symbol ‘s’ void print (float data [ ], int size = 20);
4.5

What is the main advantage of passing arguments by reference?

Answer

When we pass arguments by reference, the formal arguments in the called function become aliases to the ‘actual’ arguments in the calling function.

4.6

When will you make a function inline? Why?

Answer

When a function contains a small number of statements, then it is declared as inline function. By declaring a function inline the execution time can be minimized.

4.7

How does an inline function differ from a preprocessor macro?

Answer

The macros are not really functions and therefore, the usual error checking does not occur during compilation. But using inline-function this problem can be solved.

4.8

When do we need to use default arguments in a function?

Answer

When some constant values are used in a user defined function, then it is needed to assign a default value to the parameter.
Example :

Float area (float r, float PI = 3.1416)
    {
           return PI*r*r;
    }
4.9

What is the significance of an empty parenthesis in a function declaration?

Answer

An empty parentheses implies arguments is void type.

4.10

What do you meant by overloading of a function? When do we use this concept?

Answer

Overloading of a function means the use of the same thing for different purposes.
When we need to design a family of functions-with one function name but with different argument lists, then we use this concept.

4.11

Comment on the following function definitions:

  • (a)
    int *f( )
    { 
    int m = 1; 
    .....
    .....
    return(&m);
    }
  • (b)
    double f( )
    {
    .....
    ..... 
    return(1);
    }
  • (c)
    int & f()
    { 
    int n - 10;
    .....
    ..... 
    return(n); 
    }
Answer
No. comment
(a) This function returns address of m after execution this function.
(b) This function returns 1 after execution.
(c) returns address of n

Next Previous