Contact:-9447974300

Chapter – 3. Functions

1. What are the merits and demerits of modular programming?

Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Merits of modular programming

a). Reduces the size of the program

b). Less chance of error occurrence

c). Reduces programming complexity

d). Improves reusability

Demerits of modular programming

a). Proper breaking down of the problem is a challenging task.

b). Each sub problem must be independent of others.

2.  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.

3. What is a function? What are the different types of functions 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. int add(int a int b)

4. What is parameter or argument?

Some functions require some data for performing the task assigned to it. They are provided within the pair of parentheses of the  function name . They are known as parameters or  arguments

Eg. p=sqrt(25);

5. —- — — header file is needed for using console functions in C++.   Ans:     cstdio

6. What are console functions for character input and output operations in C++?

a). getchar():This function returns the character that is input through the keyboard. The character can be stored in a variable.          eg. char ch = getchar();

b).putchar():This function displays the character given as the argument on the standard output unit (monitor). If an integer value is given as the argument, it will be considered as an ASCII value and the corresponding character will be displayed.

Eg. char ch = ‘B’;

putchar(ch);

putchar(‘c’);

7.  What are stream functions for I/O in C++?

Stream functions allow a stream of bytes (data) to  flow between memory and objects like Key Board or Monitor

A. Input functions

a). get() :It can accept a single character or multiple characters (string) through the keyboard. To accept a string, an array name and size are to be given as arguments. Eg cin.get(str,10)

b). getline() : It accepts a string through the keyboard. The delimiter will be Enter key, the number of characters or a specified character.

Eg. cin.getline(str,len);            cin.getline(str,len,ch);

B. Output functions

a).put() :It is used to display a character constant or the  content of a character variable given as argument.

Eg. cout.put(‘B’);         cout.put(65);

b). write() : This function displays the string contained in the argument. Eg. cout.write(str,10);

8 . —- — — header file is needed for using string functions in C++.      Ans:     cstring

9 . What are the string manipulating functions in C++?

i. strlen() :  This function is used to find the length of a string(Number of characters )

Its return value is an integer.

n = strlen(str);

ii. strcpy() : This function is used to copy one string into another.

Syntax : strcpy(string1, string2);

Eg: strcpy(s1,s2);

iii. strcat() : This function is used to append one string to another string. The length

of the resultant string is the total length of the two strings.

Syntax: strcat(string1, string2);

Eg. strcat(s1,s2);

iv. strcmp() :This function is used to compare two strings. Its return value is

1.  0 if string1 and string2 are same.

2.  –ve value if string1 is alphabetically lower than string2

3.  +ve value if string1 is alphabetically higher than string2

v. strcmpi():This function is used to compare two strings ignoring cases (both the upper case and lower case letters are treated as same) syntax is same as strcmp.

10. Which header file is needed for executing mathematical functions in C++ ?

cmath

11. What are the mathematical functions in C++?

1. abs() : It takes an integer as the argument (+ve or –ve) and returns the absolute value.

2. sqrt() :

3. pow() :

12. Pick the odd one out and give reason:

a. strlen() b. pow() c. strcpy() d. strcat( )

Ans:  pow: it is a math function. All Others are string manipulating functions.

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

cmath

14. Predict the output when we compare the strings  “HELLO” and “hello” using the function strcmpi().

The return value of strcmpi( ) here is 0 because by ignoring case both strings are equal.

15. What will be output of the statement cout<<strlen(“smoking kills”); ?

Ans : 13

16. Which function converts the alphabet ‘P’ to ‘p’?

Ans: tolower( )

17. Identify the name of the function which tests whether the argument is an alphabet or number.

Ans: isalnum( )

18.  Identify the built-in functions needed for the following cases

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: abs()

19. What is the use of return statement in C++?

There are certain functions which give results after performing the task. This result is known as  return-value  of the function. This value is passed to the calling function by using  return statement. The data type of a function depends on this value. Eg. int main() ……. … return 0;

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

void function is one which does not return any value. When function is not return any value we use void data type.

 21. Distinguish between actual parameters and formal parameters.

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

22. 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

double Total(double, double);

void Math();

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;

}

Scope of a variable is that part of the program in which it is used. In the above program, scope of the variable  x  is in the  main()function because it is declared within that function. Hence this

variable cannot be used outside the function. This scope is known as  local scope.

If variable is declared before the  main()function and not within any other function, the scope

of the variable is the entire program. That is the variable can be used at any place in the program. This scope is known as  global scope.

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. A global function can be used throughout the program.

24. Distinguish between Call-by-value method and Call-by-reference method used for function calls .

Call By Value: In this parameter passing method, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in actual parameters of the caller.

Call by Reference: Both the actual and formal parameters refer to the same locations, so any changes made inside the function are actually reflected in actual parameters of the caller.

25. In C++, function can be invoked without specifying all its arguments. How?

Functions can be defined with arguments assigned with initial values . The initialized formal arguments are called  default arguments which allow the programmer to call a function with different number of arguments. That is ,we can call the function with or without giving values to the default arguments.

About the Author

Leave a Reply