Loading....
Coupon Accepted Successfully!

 

Question-1

What is a Function?

Solution:
A Function is a subprogram that acts on data and often returns a value.

Question-2

Explain briefly about the types of function.

Solution:
1. Built-in Functions. These functions are part of the compiler package. These are part of standard library made available by the compiler. For example, exit(), sqrt(), pow(), strlen() etc. are library functions (or built-in functions)

 

2. User-defined Functions. The user-defined functions are created by you, i.e., the programmer. These functions are created as per requirements of your program.

Question-3

Give the general for function definition.

Solution:
The general from of a function definition is as given below:

 

Type function-name (parameter list)

{

Body of the function

}

Question-4

Write a program to print the cute of a given number using a function.

Solution:
#include<iostream.h>

#include<conio.h>

int main( )

{ clrscr ( ) ;

float cube(float) ;                        //Function prototype declared.

Float x, y ;

Cout << "\nEnter number whose cube is to be calculated : \n" ;

Cin >> x ;

Y = cube(x) ;                           //call the function and assign

                                             //its return value to variable y.

cout << "nThe cube of" << x >> "is" << y << "\n" ;

getch( ) ;

return 0 ;

}

                                             //End of main.

float cube(float a)

{float n ;

n = a * a * a ;

return (n) ;

}

Question-5

Write about constant arguments in detail?

Solution:
Constant arguments, mean that the function cannot modify these arguments. If you pass constant values to the function, then the function cannot modify the values as the values are constants. For instance, the function call

 

length ("A string") ;

 

cannot modify its argument "A string" as it is a constant value. But if the argument being passed is not a constant, it is in the from of a variable such as

 

length (string) ;

 

where string variable may have any value. In order to make an argument constant to a function, we can use the keyword const as shown below:

 

int sum (const int a, const int b) ;

 

The qualifier const in function prototype tells the compiler that the function should not modify the argument. The constant arguments are useful when functions are called by reference.

Question-6

Write the structure for function with no argument.

Solution:
This style of function simply performs an independent task. It does not send or receive any parameters and it does not return any value. Its form is like

 

Void <function name> ( )

{

}

or

Void <function name> (void)

{

}

Question-7

Write a program to illustrate the call by value method.

Solution:
#include<iostream.h>

#include<conio.h>

void main ( )

{

        clrscr( ) ;

        int change (int) ;                        //Prototype declared.

        Int orig = 10 ;

        cout << "\nThe original value is" << orig << "\n" ;

        cout << "\nReturn value of function change ( ) is" << change(orig) << "\n" ;

        cout << "\nThe value after function change ( ) is over" <<orig << "\n" ;

        getch( ) ;

}

int change (int a)

{         a = 20;

          return a ;

}

Question-8

What is the purpose of using return statement?

Solution:
The return statement is useful in two ways. First, an immediate exit from the function is caused as soon as a return statement is encountered, and the control passes back to the operating system which is main’s caller. Second use of return statement is that it is used to return a value to the calling code.

Question-9

Write a program to illustrate the purpose of swap function.

Solution:
#include<iostream.h>

#include<conio.h>

int main( )

{

        clrscr( );

        void swap(int &, int &) ;      //Function prototype accepting two reference variables.

        Int a, b ;

        a = 7 ;

        b = 4 ;

        cout << "\nThe original values are : \n" ;

        cout << "a =" << a << ", b =" << b << "\n" ;

        swap(a, b) ;

        cout << "\nThe values after swap( ) are : \n" ;

        cout << "a =" << a << ", b =" << b << "\n" ;

        getch( ) ;

        return 0 ;

}

void swap(int &x, int &y)

{

        int temp ;

        temp = x ;

        x = y ;

        y = temp ;

        cout << "\nThe swapped values are : \n" ;

        cout << "a =" << x << ", b =" << y << "\n" ;

}

Question-10

Explain types of functions in detail.

Solution:
1. Computational Functions. The functions that calculate or compute some value and return the computed value. For example, sqrt() and cos() are computational functions. Computational functions always return a computed result.

 

2. Manipulative Functions. The functions that manipulate information and return a success or failure code. Generally, if value 0 is returned, it denotes successful operation; any other number denotes failure.

 

3. Procedural Functions. The functions that perform an action and have no explicit return value. For instance, exit() function is a procedural function.

 

The functions returning to value should be declared as returning type void so that its accidental misuse in expression(s) can be prevented.

Question-11

Explain the kinds of scope found in C++.

Solution:
1. Local Scope. A name declared in a block (i.e., {….} is local to that block and can be used only in it and the other blocks contained under it. The names of formal arguments are treated as if they were declared in the outermost block of that junction.

2. Function Scope. The variables declared in the outermost block of a function have function scope i.e., they cannot be used outside the function.

3. File Scope. A name declared outside all blocks and functions has file scope i.e., it can be used in all the blocks and functions written inside the file in which the name declaration appears.

4. Class Scope. A name of a class member has class scope and is local to its class. The details of this scope are beyond the scope of this book. Thus, we shall not explain class scope.

Question-12

Define local variable

Solution:
A variable available only to a block is called as local variable.

Question-13

What are the three steps in using a function?

Solution:
Before using a function in C++, three things that are required are:

1. Function Declaration (i.e., prototype) to specify the function’s interface to the program.

2. Function Definition to tell the program about what and how a function is doing.

3. Function call to invoke the function.

Question-14

Explain call by Reference in detail.

Solution:
Call by Reference. Using this method, the set large ( ) will be invoked by passing the reference of original variables so that the function set large ( ) works with the original values rather than creating its own copy of the values. The code using this method will look like as follows:

void set large (int & a, int & b) ;

int main( )

{

        int a, b ;

        cin >> a >> b ;

        set large (a, b) ;

}

void setlarge (int & a, int & b)

{

        if (a > b)

        a = –1 ;

        else

        b = –1 ;

}

Question-15

Define Scope.

Solution:
The program part(S) in which a function or a variable is accessible.




Test Your Skills Now!
Take a Quiz now
Reviewer Name