Home | | Computer Science 11th std | Answer the following questions

Functions | Computer Science - Answer the following questions | 11th Computer Science : Chapter 11 : Functions

Chapter: 11th Computer Science : Chapter 11 : Functions

Answer the following questions

Short Answers, Explain in Brief, Explain in detail, Important Questions, Hands on practice, Write C++ program to slove the following problems - Computer Science : Introduction to C++: Functions

Introduction to C++

Functions


Part –II

Answer to all the questions (2 Marks):


1. Define Functions.

Answer: A large program can typically be split into small subprograms (blocks) called as functions where each sub-program can perform some specific functionality. Functions reduce the size and complexity of a program, makes it easier to understand, test, and check for errors.


2. Write about strlen() function.

Answer: The strelen() takes a null terminated byte string source as its argument and returns its length. The length does not include the null(\0) character.


3. What are importance of void data type.

Answer:

Void type has two important purposes:

(i) To indicate the function does not return a value

(ii) To declare a generic pointer.


4. What is Parameter and list its types?

Answer:

(i) Arguments or parameters are the means to pass values from the calling function to the called function.

(ii) The variables used in the function definition as parameters are known as formal parameters.

(iii) The constants, variables or expressions used in the function call are known as actual parameters.


5. Write a note on Local Scope.

Answer:

(i) A local variable is defined within a block. A block of code begins and ends with curly braces {}

(ii) The scope of a local variable is the block in which it is defined.

(iii) A local variable cannot be accessed from outside the block of its declaration.

(iv) A local variable is created upon entry into its block and destroyed upon exit.

 

Part – III

Answer to all the questions (3 Marks):


1. What is Built-in functions ?

Answer: C++ provides a rich collection of functions ready to be used for various tasks. The tasks to be performed by each of these are already written, debugged and compiled, their definitions alone are grouped and stored in files called header files. Such ready-to-use sub programs are called pre-defined functions or build-in functions.

 

2. What is the difference between isuppr() and toupper() functions ?

Answer:


isuppr()

• This function is used to check the given character is upper case

• This function will return 1 if true otherwise ()

• Example : isupper('a') will return 0

toupper()

• This function is used to convert the given character into its uppercese

• This function will return the uppercase equivalent of the given character

• Example : toupper('a') will return 'A'


3. Write about strcmp() function.

Answer: The strcmp() function takes two arguments: string1 and string2. It compares the contents of string1 and string2 lexicographically.

The strcmp() function returns a:

(i) Positive value if the first differing character in string1 is greater than the corresponding character in string2. (ASCII values are compared).

(ii) Negative value if the first differing character in string1 is less than the corresponding character in string2.

(iii) 0 if string1 and string2 are equal.


4. Write short note on pow() function in C++.

Answer: The pow() function returns base raised to the power of exponent. If any argument passed to pow() is long double, the return type is promoted to long double. If not, the return type is double. The pow() function takes two arguments.

(i) base- the base value

(ii) exponent - exponent of the base.


5. What are the information the prototype provides to the compiler ?

Answer:

(i) The return value of the function is of type long.

(ii) fact is the name of the function

(iii) The function is called with two arguments:

The first argument is of int data type

The second argument is of double data type. The function prototype provides details about the return data type, name of the function and a list of formal parameters or arguments.


6. What is default arguments ? Give example.

Answer: In C++, one can assign default values to the formal parameters of a function prototype. The Default arguments allows to omit some arguments when

calling the function.

Example: void default value(int n1 = 10, n2= 100);

 

Part –IV

Answer to all the questions (5 Marks):


1. Explain Call by value method with suitable example.

Answer: This method copies the value of an actual parameter into the formal parameter of the function. In this case, changes made to formal parameter within the function will have no effect on the actual parameter

Example :

#include<iostream>

using namespace std;

void display(int x)

{

int a=x*x;

cout<<"\n\nThe Value inside display function (a*a):"<<a;

}

int main()

{

int a;

cout<<"\nExample : Function call by value:";

cout<<"\n\nEnter the Value for A :";

cin>>a;

display(a);

cout<<"\n\nThe Value inside main function"<<a;

return(0);

}

Output:

Function call by value

Enter the Value for A:5

The Value inside display function (a * a) : 25

The Value inside main function 5


2. What is Recursion? Write a program to find GCD using recursion.

Answer: A function that calls itself is known as recursive function. And, this technique is known as recursion.

 Example :

#include<iostream>

 using namespace std;

//Function to find HCF //

int hcf(int nl, int n2)

{

if (n2 !=0)

return hcf(n2, nl) % n2);

else

return n1;

}

int main()

{

int numl, num2;

cout<<"Enter two positive integers:”;

cin >> num 1 >> num 2;

cout<<"Highest Common Factor (HCF) of “<<numl;

cout<<"&" << num2 << "is:" << hcf(numl, num2);

return((0);

}

Output:

Enter two positive integers : 350 100

Greatest common Divisor(GCD) of : 350 & 100 is : 50


3. What are the different forms of function return? Explain with example.

Answer: Returning from the function is done by using the return statement. The return statement stops execution and returns to the calling function. When a return statement is executed, the function is terminated immediately at that point.

Different forms of function return:

(i) The return statement

(ii) The Returning values

(iii) The Returning by reference

(i) The return statement: The return statement is used to return from a function. It is categorized as a jump statement because it terminates the execution of the function and transfer the control to the called statement. A return may or may not have a value associated with it. If return has a value associated with it, that value becomes the return value for the calling statement. Even for void function return statement without parameter can be used to terminate the function.

Syntax: return expression / variable;

Example : return(a+b); return(a); return;

(ii) The Returning values : The functions that return no value is declared as void. The data type of a function is treated as int, if no data type is explicitly mentioned.

For Example : int add (int, int);

add (int, int);

In both prototypes, the return value is int, because by default the return value of a function in C++ is of type int.

(iii) The Returning by reference :

#include<iostream>

using namespace std;

 int main()

{

int n1 = 150;

 int &nlref=nl;

cout<<"\n The Value of Nl = "<<nl<<" and n1 Reference = "<<n1 ref;

nlref++;

cout<<"\n After n1 increased the Value of Nl = "<<nl;

cout<<" and n1 Reference = "<<n1ref;

return(0);

}

Output:

The Value of Nl = 150 and nl Reference = 150

 After nl increased the Value of Nl = 151 and nl Reference =151


4. Explain scope of variable with example.

Answer:

(i) A scope is a region or life of the variable and broadly speaking there are three places, where variables can be declared,

(ii) Inside a block which is called local variables.

(iii) Inside a function is called function variables.

(iv) Outside of all functions which is called global variables.

(v) Inside a class is called class variable or data members.

1. Local Scope :

(i) A local variable is defined within a block. A block of code begins and ends with curly braces {}

(ii) The scope of a local variable is the block in which it is defined.

(iii) A local variable cannot be accessed from outside the block of its declaration.

Example :

#include<iostream>

 using namespace std;

int main ()

{

int a,b;

a = 10;

b = 20;

if (a > b)

{

int temp;            //local to this if block//

temp = a;

a = b;

b = temp;

}

Cout<<"\n descending order../n";

Cout<<a<<"t"<<b;

return (0);

2. Function scope :

(i) The scope of variables declared within a function is extended to the function block, and all sub-blocks therein.

(iii) The life time of a function scope variable, is the life time of the function block. The scope of formal parameters is function scope.

Example :

#include<iostream>

 using namespace std;

void add(int x, int y)

{

int m=x+y;            //'m' declared within function add()//

cout<<"\nThe Sum ="<<m;

}

int main()

{

int a, b;

a= 10;

b = 20;

add(a,b);

cout<<m;              //'m' declared within function add()//

3. File Scope :

(i) A variable declared above all blocks and functions (including main()) has the scope of a file. The life time of a file scope variable is the life time of a program.

(ii) The file scope varible is also called as global variable.

Example :

#include<iostream>

using namespace std;

int file_var+20;                        //Declared within File//

void add(int x, int y)

{

int m=x+y+file_var;

cout<<"\n The Sum = "<<m;

}

int main ()

{

int a, b;

a = 10; b = 20;

 add(a,b);

cout<<"\nThe File Variable = "<<file_var;

return(0);

}

Output:

The Sum = 50

The File Variable = 20

4. Class scope:

(i) A class is a new way of creating and implementing a user defined data type. Classes provide a method for packing together data of different types.

(ii) Data members are the data variable that represent the features or properties of a class.

class student

{

private :

int markl, mark2, total;

};

The class student contains markl, mark2 and total are data variable. Its scope is within the class student only.


5. Write a program to accept any integer number and reverse it.

Answer:

#include<iostream.h>

using namespace std;

int main()

{

int n, d, s= 0;

cout<<"Enter a number"<<endl;

cin>>n;

while (n! = 0)

}

d = n % 10; s = (s* 10) +d;

n = n/10;

}

Cout<<"The reversed number is"<<s;

}

Using Recursion

 #include<iostream.h>

 int reverse (int x)

 int d, s= 0;

{

if(x == 0)

return s;

else

{ d = x% 10;

s = (s * 10)+d;

} reverse (n/10);

}

void main ()

{

int n, y;

cout<<"Enter a number "<<endl;

cin>>n;

y = reverse(n);

}

cout <<"Reversed Number is"<<y;

}


Hands on practice:

Write C++ program to slove the following problems :

 

1. Program that reads two strings and appends the first string to the second. For example, if the first string is entered as Tamil and second string as nadu, the program should print Tamilnadu. Use string library header.

Answer:

Coding :

#include<iostream>

#include<stdio.h>

 using namespace std;

int main()

{

char s1 [ ]=”Tamil”,s2[ ]=”nadu”;

strcat(sl,s2);

puts(sl);

system("pause");

return 0;

}

 

2. Program that reads a string and converts it to uppercase. Include required header files.

Answer:

#include <iostream>

#include <ctype.h>

 using namespace std;

 int main()

{

char ch;

cout<<"Enter the Character";

 ch=getchar();

cout<<"The Character is changed to Upper Case="<<toupper(ch);

system("pause");

 return 0;

}

 

3. Program that checkos whether a given character is an alphabety or not. If it is an alphabet, whether it is lowercase character or uppercase character? Include required header files.

Answer:

#include<iostream>

#include<ctype.h>

using namespace std;

int main()

{

char ch;

cout<<"Enter character"<<endl;

cin>>ch;

if (isalpha(ch))

cout<<"It is an alphabet"<<endl;

else

cout<<"It is not an alphabet"<<endl;

if(isupper(ch))

cout<<"It is in uppercase"<<endl;

else

cout<<"It is in lowercase"<<endl;

return 0;

}

 

4. Program that checks whether the given character is alphanumeric or a digit. Add appropriate header file.

Answer:

#include<iostream.h>

#include<stdio.h>

#include<ctype.h>

 int main()

{

char ch;

cout<<"Enter a character"<<endl;

 cin>>ch;

if (isalnum(ch))

cout<<"It is alphanumeric"<<endl;

else

cout<<"It is a digit"<<endl;

return 0;

}

 

5. Write a function called zero_small ( ) that has two integer arguments being passed by reference and sets smaller of the two numbers to 0. Write the main program to access this funtion.

Answer:

#include<iostream>

using namespace std;

 void zero_small(int &,int &);

 int main()

{

int x,y;

cout<<"Enter first number :”;

 cin>>x;

cout<<"Enter second number : “;

cin>>y;

zero_small(x,y);

cout<<"First number is : "<<x;

cout<<"\nSecond number is : "<<y;

system("pause");

return 0;

}

void zero_small(int &a, int &b)

{

if(a<b)

a=0;

else

b=0;

}

 


6. Write definition for a function sumseries ( ) in c++ with two arguments/ parameters - double x and int n. The function should return a value of type double and it should perform sum of the following series:

x-x2 /3! + x3/5! - x4 / 7! + x5/9! -... upto n terms.

Answer:

#include<math.n>

void sumseries(double x, int n)

{

double sum=0, sign= −l;

 int i, c=l;

for (i = 1; i<=n; i++)

{

int f= 1;

for (int j=l; j<=c;j++)

{f = f*j ;}

sign = sign * −1;

sum = sum + (sign * (pow(x,i)/f);

c = c+2;

}

Cout<<"Sum of the series="<<sum;

}

int main ()

{

double x;

 int n;

cout<<"Enter the value of x"<<endl;

 cin>>x;

cout<<"Enter the number of terms"<<endl;

cin>>n;

sumseries(x, n);

 return 0;

}

 

7. Program that invokes a function calc ( ) which intakes two intergers and an arithmetic operator and prints the corresponding result.

Answer:

#include<iostream>

void calc (int a, int b, char ch)

{

if (ch == '+') cout<<a+b;

if (ch ==’ −’)cout<<a −b;

 if (ch == '*') cout<<a*b;

 if (ch = ’/') cout<<a/b;

if (ch == '%') cout<<a%b;

}

int main ()

{

int a, b;

char ch;

cout<<"Enter first number"<<endl;

cin>>a;

cout<<"Enter second number"<<endl;

 cin>>b;

cout<<"Enter Arithmetic operator"<<endl;

cin>>ch;

calc(a, b, ch);

}

 

Tags : Functions | Computer Science , 11th Computer Science : Chapter 11 : Functions
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 11 : Functions : Answer the following questions | Functions | Computer Science


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.