Pageviews

Friday, 12 July 2019

+2].3. Functions - Solved questions from text book


                                                              Chapter 3. Functions
(+2 Computer Application , Text book Questions and Answers)


1.  What is modular programming?

     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. Each sub task will be considered as a module and we write programs for each module. The process of breaking large programs into smaller sub programs is called modularization.

2. What is meant by a function in C++?

    Function is a named unit of statements in a program to perform a specific task as part of the solution.  There are two types of functions in C++.

 a). Predefined functions or Built in Functions : Functions that are already written , compiled and their definitions are grouped and stored in header files.
  Eg. sqrt(),  toupper()

b). User Defined Functions: Functions that are written by the user to carry on some task.
  Eg.  main( )

3.  Name the header file required for using character functions.

  ctype

4.  Predict the output of cout<<sqrt(49).

output
   7

5. Pick the odd one out and give reason:

a. strlen()

b. pow()

c. strcpy()

d. strcat( )

Ans b.

6.   Name the header file needed for the function pow( ). 

    math

7. Predict the output when we compare the strings "HELLO" and "hello" using the function strcmpi(). 

Returns strings are same.Because  the uppercase and lowercase letters will be treated as same during the comparison.

8.   What will be output of the statement
 cout<<strlen("smoking kills"); ? 

output

 13

9. Which function converts the alphabet 'P' to 'p'? 

    tolower( )

10.  Identify the name of the function which tests whether the argument is an aphabet or number.

      isalpha( )

11. Identify  the most essential function in C++ programs?

      function is a group of statements that together perform a task. Every C++ program has at least one function, which is main(), and all the most trivial programs can define additional functions.

12.  List the three elements of a function header.

data_type function_name(argument_list)
 {
 statements in the body;
 }

The data_type is any valid data type of C++. The function_name is a userdefined word (identifier). The argument_list, which is optional, is a list of parameters, i.e. a list of variables preceded by data types and separated by commas. The body comprises of C++ statements required to perform the task assigned to the function.

13.  What is function prototype? 
         
        . 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. 
data_type function_name(argument_list);

14.  Which component is used for data transfer from calling function to called function? 

          Parameters or Arguments

 15. What are the two parameter passing techniques used in C++? 

         Call by value and Call by reference.

16. Identify the name of the function call where & symbol is used along with formal arguments.

      Reference variable

17.  The built-in function to find the length of a string is  _________. 

      strlen( )

18.  Write down the role of header files in C++ programs.

       In C++ the header file contain the group of function declarations that may need for the proper working of a program. So there is no need to add the function definitions of all the functions separately. 
Eg.  cmath  for mathematical functions. 

19.  When will you use void data type for function definition? 

        In most of the functions, return is placed at the end of the function. The functions defined with void data type may not have a return statement within the body. But if we use return statement, we cannot provide any value to it.Void means nothing to return datatype.

20.  Distinguish between actual parameters and formal parameters. 

         The arguments given at the calling of a function is called actual (original) arguments or actual parameters since they are the actual data passed to the function for processing.   
 Eg.  a = SimpleInterest(x, y, z);
 The arguments used in the function definition are known as formal parameter. 

 Eg.  float SimpleInterest(int P, int N, float R) 

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

Ans.  a).      double Total (double  ,  double );
  b).      void Math(); 

22.   Discuss the scope of global and local variables with examples.

        A local variable  is one that declared within a function or a block of statements. It is available only within that function or block.
 If a variable  is declared before the main() function t is the function can be used at any place in the program. This scope is known as global scope. Also declaration of a variable outside  all functions also have global scope.

23.  How a local function differs from a global function?

 A function which is declared inside the function body of another function is called   a  local function as it can be used within that function only. A function declared outside the function body of any other function is called a   global function and its scope is the entire program



eg. #include <iostream>

using namespace std;

int cb; //global variable

void test() //global function

{

int cube(int n); // local function

cb=cube(x); //Invalid call. x is local to main()

cout<<cb;

}

int main()

{

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;

} 24.  Identify the built-in functions needed for the following cases



a. To convert the letter 'c' to 'C'

b. To check whether a given character is alphabet or not.

c. To combine strings "comp" and "ter" to make "computer"

d. To find the square root of 25

e. To return the number 10 from -10




a. To convert the letter 'c' to 'C'

Ans: toupper()

b. To check whether a given character is alphabet or not.

Ans: isalpha()

c. To combine strings "comp" and "ter" to make "computer"

Ans: strcat()

d. To find the square root of 25

Ans: sqrt()

e. To return the number 10 from -10

Ans: ab

25.  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); 

26.  The prototype of a function is: int fun(int, int); 

The following function calls are invalid. Give reason for each. 
a. fun("hello",4);
 b. cout<<fun(); 
c. val = fun(2.5, 3.3); 
d. cin>>fun(a, b); 
e. z=fun(3); 

Ans:  a). In Prototype declaration formal variables are integers but actual parameters contains string type.
  b). No actual parameters. 
 c). Actual parameters are not integer type.  
d). calling a function by cin is error. 
 e). Not enough actual parameters. 

27.  Consider the following program and predict the output if the radius is 5. Also write the reason for the output.

#include<iostream>
using namespace std;
float area(float &);
int main()
{
float r, ans;
cout<<"Enter radius :";
cin>>r;
ans = area(r);


cout<<area;
cout<<r;
return 0;
}
float area(float &p)
{
float q;
q = 3.14 * p * p;
 p++;
return q;
}

         output

 Enter the radius: 78.5