PLUS ONE COMPUTER SCIENCE
First year computer science Solved Questions and answers
10 - Functions
1. What is modular programming?
Ans. In programming, the entire problem will be divided into small sub problems that can be solved by writing separate programs. This kind of approach is known as modular programming.
Ans. In programming, the entire problem will be divided into small sub problems that can be solved by writing separate programs. This kind of approach is known as modular programming.
2. What is a function in C++?
Ans. function is a named unit of statements in a program to perform a specific task as part of the solution. It is not necessary that all the functions require some parameters and all of them return some value. C++ provides a rich collection of functions ready to use for various tasks. The functions clrscr(), getch(), sqrt(), etc. are some of them.
Ans. function is a named unit of statements in a program to perform a specific task as part of the solution. It is not necessary that all the functions require some parameters and all of them return some value. C++ provides a rich collection of functions ready to use for various tasks. The functions clrscr(), getch(), sqrt(), etc. are some of them.
3. Name the header file required for using character functions.
Ans. cctype
Ans. cctype
4. Name the function that displays the subsequent data in the specified width.
5. Pick the odd one out and give reason:
(a) strlen() (b) itoa() (c) strcpy() (d) strcat()
Ans. b
Ans. b
6. Identify the most essential function in C++ programs.
Ans. main( )
Ans. main( )
7. List the three elements of a function header.
Ans. The three elements of a function header are return data type,function name and function argument list
Ans. The three elements of a function header are return data type,function name and function argument list
8. What is function prototype?
Ans. A function prototype is the declaration of a function by which compiler is provided with the information about the function such as the name of the function, its return type, the number and type of arguments, and its accessibility. This information is essential for the compiler to verify the correctness of the function call in the program. This information is available in the function header and hence the header alone will be written as a statement before the function call.
The following is the format: data_type function_name(argument_list);
Ans. A function prototype is the declaration of a function by which compiler is provided with the information about the function such as the name of the function, its return type, the number and type of arguments, and its accessibility. This information is essential for the compiler to verify the correctness of the function call in the program. This information is available in the function header and hence the header alone will be written as a statement before the function call.
The following is the format: data_type function_name(argument_list);
9. Which component is used for data transfer from calling function to
called function?
Ans. Arguments or parameters
Ans. Arguments or parameters
10. What are the two parameter passing techniques used in C++?
Ans. Call by value and Call by reference
Ans. Call by value and Call by reference
11. If the prototype of a function is given immediately after the
preprocessor directive, its scope will be _______.
Ans. Global
12. What is recursion?
Ans. Usually a function is called by another function. Now let us define a function that calls itself. The process of calling a function by itself is known as recursion and the function is known as recursive function
Ans. Usually a function is called by another function. Now let us define a function that calls itself. The process of calling a function by itself is known as recursion and the function is known as recursive function
13. What is the scope of predefined functions in C++?
Ans. Global
Ans. Global
14. The arguments of a function have ______ scope.
Ans. Local
Ans. Local
15. How do top down design and bottom up design differ in programming?
16. What is a function in C++?
17. The ability of a function to call itself is _________.
Ans. Recursion
Ans. Recursion
18. Write down the role of header files in C++ programs.
Ans. hpp extension or no extension at all. The primary purpose of a header file is to propagate declarations to code files. Header files allow us to put declarations in one location and then import them wherever we need them. This can save a lot of typing in multi-file programs
Ans. hpp extension or no extension at all. The primary purpose of a header file is to propagate declarations to code files. Header files allow us to put declarations in one location and then import them wherever we need them. This can save a lot of typing in multi-file programs
19. When will you use void data type for function definition?
Ans. Many programming languages need a data type to define the lack of return value to indicate that nothing is being returned. The void data type is typically used in the definition and prototyping of functions to indicate that either nothing is being passed in and/or nothing is being returned.
Ans. Many programming languages need a data type to define the lack of return value to indicate that nothing is being returned. The void data type is typically used in the definition and prototyping of functions to indicate that either nothing is being passed in and/or nothing is being returned.
20. Distinguish between actual parameters and formal parameters.
Ans. Arguments or parameters are the means to pass values from the calling function to the called function. The variables used in the function definition as arguments are known as formal arguments. The constants, variables or expressions used in the function call are known as actual (original) arguments. If variables are used in function prototype, they are known as dummy arguments.
Ans. Arguments or parameters are the means to pass values from the calling function to the called function. The variables used in the function definition as arguments are known as formal arguments. The constants, variables or expressions used in the function call are known as actual (original) arguments. If variables are used in function prototype, they are known as dummy arguments.
21. Construct the function prototypes for the following functions
(a) Total() takes two double arguments and returns a double
(b) Math() takes no arguments and has no return value
22. Distinguish between exit() function and return statement.
23. Discuss the scope of global and local variables with examples.
#include <iostream>
using namespace std;
int cb; //global variable
void test()//global function since defined above other functions
{
int cube(int n); //It is a local function
cb=cube(x); //Invalid call. x is local to main()
cout<<cb;
}
int main() // beginning of main() function
{
int x=5; //local variable
test(); //valid call since test() is a global function
cb=cube(x); //Invalid call. cube() is local to test()
cout<<cb;
}
int cube(int n)//Argument n is local variable
{
int val= n*n*n; //val is local variable
return val;
}
#include <iostream>
using namespace std;
int cb; //global variable
void test()//global function since defined above other functions
{
int cube(int n); //It is a local function
cb=cube(x); //Invalid call. x is local to main()
cout<<cb;
}
int main() // beginning of main() function
{
int x=5; //local variable
test(); //valid call since test() is a global function
cb=cube(x); //Invalid call. cube() is local to test()
cout<<cb;
}
int cube(int n)//Argument n is local variable
{
int val= n*n*n; //val is local variable
return val;
}
ref page no.303.10th chapter .
24. Distinguish between Call-by-value method and Call-by-reference method used
for function calls.
Call by Value Method Call by Reference Method
Ordinary variables are used as formal parameters. Reference variables are used as formal parameters.
• Actual parameters will be variables only. • The changes made in the formal arguments are reflected in actual arguments. • Memory of actual arguments is shared by formal arguments
• Actual parameters may be constants,
variables or expressions.
• The changes made in the formal arguments
are not reflected in actual arguments.
• Exclusive memory allocation is
required for the formal arguments.
Call by Value Method Call by Reference Method
Ordinary variables are used as formal parameters. Reference variables are used as formal parameters.
• Actual parameters will be variables only. • The changes made in the formal arguments are reflected in actual arguments. • Memory of actual arguments is shared by formal arguments
• Actual parameters may be constants,
variables or expressions.
• The changes made in the formal arguments
are not reflected in actual arguments.
• Exclusive memory allocation is
required for the formal arguments.
25. In C++, function can be invoked without specifying all its arguments. How?
26. Write down the process involved in recursion.
A typical recursive function will be as follows:
int function1()
{
............ ............
int n = function1(); //calling itself
............ ............
}
In recursive functions, the function is usually called based on some condition only.
int factorial(int n)
{
if((n==1)||(n==0))
return 1;
else if (n>1)
return (n * factorial(n-1));
else
return 0;
}
f = factorial(3);
return (3 * factorial(3-1));
In order to find 3 * factorial(2), it needs to find the value of factorial(2).
return (2 * factorial(2-1));
In order to find this returning value, it calls the factorial() function again with 1 as the value of the parameter n. Now, the condition in the if statement becomes true, hence it will return 1 as the value of the function call factorial(1);
return 2;
: return 3 * 2;
Now the value 6 is returned as the value of the function call factorial(3);.
A typical recursive function will be as follows:
int function1()
{
............ ............
int n = function1(); //calling itself
............ ............
}
In recursive functions, the function is usually called based on some condition only.
int factorial(int n)
{
if((n==1)||(n==0))
return 1;
else if (n>1)
return (n * factorial(n-1));
else
return 0;
}
f = factorial(3);
return (3 * factorial(3-1));
In order to find 3 * factorial(2), it needs to find the value of factorial(2).
return (2 * factorial(2-1));
In order to find this returning value, it calls the factorial() function again with 1 as the value of the parameter n. Now, the condition in the if statement becomes true, hence it will return 1 as the value of the function call factorial(1);
return 2;
: return 3 * 2;
Now the value 6 is returned as the value of the function call factorial(3);.
27. Look at the following functions:
int sum(int a,int b=0,int c=0)
{
return (a + b + c);
}
(a) What is the speciality of the function regarding the parameter list?
(b) Give the outputs of the following function calls by explaining its working
and give reason if the function call is wrong.
i) cout<<sum(1, 2, 3);
(ii) cout<<sum(5, 2);
(iii) cout<<sum();
(iv) cout<<sum(0);
28. The prototype of a function is: int fun(int, int);
The following function calls are invalid. Give reason for each.
(a) fun(2,4);
(b) cout<>fun(a, b);