[#6] – Functions – Questions


A C++ program is made up of structured sequence of statements. If a program is very large and complex, there are certain group of statements which are repeated again and again at different places in a program. Such programs are very difficult to write, debug, understand, test and maintain. To implement such programs efficiently, we need to break down a large program into smaller sub-programs which can easily be designed, implemented and maintained.


These sub-programs are called functions. In C++, programmers use functions as a building blocks to construct large programs. The functions are compiled along with the main program to form a complete program. This process of dividing a large program into functions which work independently is known as Modular Programming. Using this technique, we can group together repeated set of instructions which can be placed within a single functions. This can be accessed throughout the program whenever, it is needed.

Key Points To Remember –

  1. A function is a self contained block of statements that perform a specific task. A function groups a number of repeated statements in a program into a single unit and is identified by a name. Thus, function helps to reduce the size of program by calling and using functions at different places in a program.
  2. Functions can be categorized as library and user-defined functions. The library functions are predefined and precompiled functions that are designed to perform some specific tasks. The user-defined functions are defined by the user which consists of three components: Function Definition, Function Declaration and Function call.
  3. Function Definition is that part of function where the actual code of the function is defined, he function call transfer control to the function and function declaration gives the compiler the details about the functions such as the number, a type of arguments and the return type.
  4. The arguments used in the function call are called the actual arguments. The arguments represented in the called function are called the formal arguments.
  5. On calling a function, the control is transferred from the calling function to the called function. When the function being called completes its execution, the control is transferred back to the calling function.
  6. Arguments can be passed to the function(s) by value, by address or by reference. In Pass by value mechanism, subsequent changes to the formal parameter do not affect the actual argument. The function works with the copy of the argument. In pass by address or reference, the function works with the original argument, subsequent changes made to the formal parameter affect the actual parameter.
  7. Function can return only ne value but it is possible to return more than one value using the concept of references.
  8. A reference variable must be initialized when it is declared.
  9. C++ provides the concept of function overloading in which a family of functions share a common name that perform multiple activities. The compiler matches the function call on the basis of number of argument(s) passed and if the number of argument(s) are same then depending upon the type of argument(s) passed to the function.
  10. When the function is specified as inline, the compiler replaces the function call with the corresponding function code. Normally, the inline mechanism is meant to optimize frequently called small functions.
  11. An inline function must be defined before it is called.
  12.  C++ provides the facility to use default value(s) for the argument(s) which are specified in function declaration when they are omitted in the function call. These arguments are known as default arguments which are specified left to right.
  13. Every variable in C++ is associated with a scope and a storage class in addition to its data type. The scope or visibility of a variable is the portion of the program where a variable can be used. The storage class of variable specify how long a variable remains in memory. It determines the lifetime during which memory is associated with a variable.
  14. There are four types of storage class specification: auto, extern, static, and register.
  15. auto is the most common storage class specification. Local variables have the auto storage class by default. The scope of an automatic variable is limited to the block or function in which it is defined. Automatic variable(s) are created automatically when the control enters the function (or block) and are destroyed automatically when the control exits the function (or block).
  16. In multi-file program, the global variable(s) is defined only once in the source file and must be declared using extern keyword in every file that uses it. The extern keyword informs the compiler that the variable thus qualified is actually one of the same name defined in some other file.
  17. A variable with a static storage class specification has scope limited to the function (or block) in which it is defined and preserves its value for the entire duration of the program.
  18. Automatic variables in a function are normally stored in the memory, but in order to speed up the access to the variable we store it in registers using register storage class specification.
  19. Recursion is a process in which a function calls itself repeatedly, until some specified condition is met. It is useful for writing repetitive problems where each action is stated in terms of the previous results.
  20. A local variable is a variable which is defined inside a function block.
  21. A global variable is a variable defined above all the functions in a program. A global variable can be defined once.

Viva Voce Questions – 

Here are commonly asked questions with answers:

  1. What does a header file contain ?
    • A header file contains the declarations of library functions whose definitions are present in the library file. For example . The header file “math.h” contains declaration of various mathematical library function such as sqrt(), pow () etc.
  2. Is it necessary to include a function declaration for user-defined function ?
    • If the function call precedes the function definition then we must declare the function before using it. However, if the function definition proceeds the function call(s) then there is no need to include function declaration.
  3. What are the different ways of passing arguments to a function in C++ ?
    • Passing by Value
    • Passing by Reference
    • Passing by Pointers
  4. What is the output of the following program segment ?
    int i=10;
    int &j=i;

    • 5 as ‘j’ is a reference variable of i.
  5. What is the use of reference variables ?
    • Reference variables are used to implement pass by reference parameter passing mechanism in C++
  6. What is the difference between passing argument by value to that of reference ?
    • In case of pass by value, any modification to the formal argument(s) in the called function are not reflected to the actual argument in the calling program whereas in case of pass by reference , modification made in the called function are reflected in the calling program.
  7. Can we return more than one value from a function ?
    • Using return statement, a function can return only one value. However, a function can return more than one value, using pass by Reference or pass by pointer parameter passing techniques.
  8. If the user simply uses a return statement without any value then what will be the return type of the function ?
    • void
  9. Can you call a function in the cout statement ?
    • Yes, you can call a function in the cout statement provided function doesn’t have void return type i.e. function should return something.
  10. Can two functions with same name and parameter list be distinguished on the basis of their return type ?
    • No, Overloaded functions cannot be distinguished on the basis of their return type.
  11. What is name mangling ?
    • It is the process in which name of the functions in the source file is converted to internally different used names. It is usually done in function overloading where the compiler changes the name of all overloaded function definitions so as to implement function overloading.
  12. Is there any ambiguity while overloading functions ?
    • Yes, when two overloaded functions have an argument of same type but differ in technique of parameter passing. If we call these functions like int k=10, the compiler in no way distinguish which version of the overloaded function will be called.
  13. What is the difference between iterative and recursive programming ?
    • An iterative function is a function that uses a loop to repeat an action whereas a recursive function is a function that calls itself repeatedly.
  14. What is the output of the following program containing macro ?
    # include< iostream.h>
    #define SQUARE (x)  x*x
    int main ()
    int b = square (4.5+3.5);
    return 0;

    • 45.75

Other Similar Questions – 

  1. What is a function ? Why do we need it ?
  2. Discuss the advantages of using functions ?
  3. What are library functions ? Give examples of few library functions ?
  4. What is a user- defined function ? Why are they needed ?
  5. What is the difference between a library and an user-defined function ? Why are they needed ?
  6. What is the purpose of function declaration ? Can we omit it ?
  7. What do you mean by a function call ? From what parts of a program can a function be called ?
  8. What are different ways of writing function declarations ?
  9. What do you mean by actual and formal arguments ? explain the relationship between them ?
  10. Describe some alternate terms used in place of formal argument(s) and actual argument(s) ?
  11. When is function declaration required ?
  12. List out the rules governing the use of return statement ?
  13. What are various techniques used for passing arguments ?
  14. What is the difference between Pass by value and Pass by Reference ? Give Examples.
  15. What are the advantages and disadvantages of Pass by Reference ?
  16. How can you return more than one value from a function ?
  17. How will you return a value by reference ?
  18. List some important points while using functions ?
  19. What do you mean by function overloading ? When do we need it ?
  20. In which ways declaration of function overloading differs from normal functions ?
  21. What are the advantages of function overloading ?
  22. Give the steps for selection of appropriate overloaded function ?
  23. Define inline function ? When will you make a function inline ?
  24. How inline function is different from a preprocessor macro ?
  25. Does inline function improve the execution speed of program ? Discuss.
  26. Discuss the advantages and disadvantages of inline functions ?
  27. Discuss with an example how effect of default arguments can be alternatively achieved by function overloading ?
  28. When do we need to use default arguments in a function ?
  29. How is the function containing default arguments evaluated ?
  30. What are the restrictions on the use of inline functions ?
  31. Define scope and lifetime of a variable.
  32. Explain different storage class specifications ?
  33. Define static variable ? Discuss its lifetime and visibility ?
  34. Distinguish between the following:
    • Actual and Formal arguments
    • Global and local variables
    • Automatic and static variables
    • Global and extern variables
  35. How a user-defined function differs from other user defined functions ?
  36. What is the return type of main() ? Why ?
  37. What are the register variables ? Why do we use it ?
  38. In a multi-file program, how many times global variables and extern variables needs to be defined and declare ? Why ?
  39. What is an automatic variable ? When do we use it ?
  40. Define Recursion ? What type of programs need to solved recursively.
  41. What are advantages and disadvantages of recursion ?
You may also like:

Related Posts

Leave a Reply