Function in C++

Function in C++

FUNCTION in C++

A function in c++ is a group of statement that together performs a particular task. Every C++ program contains a function, that is the main function. Main () is the entry point of the program. At this point, the execution of the program is started.

Syntax:-

void main()
{
----------
----------
}

What is reference?

Reference is an inter pointer. Reference looks like ordinary but works like a pointer.

Uninitialized reference is an error. Reference must be initialized at the time of declaration.             Reference not exists in c but exists in c++.

int x = 10;                         int &r = x;

What is call by value? call by address, and call by reference?

If the formal parameter is ordinary type i.e. called call by value.

If the formal parameter is pointer type i.e. called call by address.

If the formal parameter is reference type i.e. called call by reference.

What is call by reference? Explain through an example?

In call by reference, the formal parameter is reference type.

In call by reference for same information different copy is not created.

Reference is an internal pointer.

Reference looks like ordinary but works line pointer.

Example:
#include”iostream”

using namespace std;

void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
main()
{
int x = 10;
int y = 20;
cout << “Before swap : ” << x << ‘ ‘ << y << endl;
swap(x, y);
cout << “After swap : ” x << ‘ ‘  << y << endl;
}

What is return by reference? Explain through example?

The merits of using function are:

  1. time complexity is reduced
  2. readable
  3. reusability
  4. maintainability

The demerits  of using the function are:

It increases the time complexity.

To avoid the demerit of using the function in c++ inline keyword is required.

inline must give at the function declaration time.

Example:

#include”iostream”

using namespace std;

inline int add(int , int );

int add(int x, int y) { return x + y;}

main()

{

int x = 10;

int y = 20;

cout << add(x, y);

}

What is default arguments in c++? Explain through an example?

If we pass less arguments then the required arguments then too less arguments error will come.

To avoid the too less arguments error default argument concept is required.

Default arguments must be given at the function definition time.

If the arguments are not passed then default arguments will be activated.

Example:

#include”iostream”  using namespace std;

int add(int x =0; int y =0 ; int z = 0)
{
return x + y + z;
}
main()
{
cout << add() << endl;                     // output 0                       
cout << add(10) << endl;                // output 10                                           
cout << add(10, 20) << endl;             // output 30                                          
cout << add(10,20,30) << endl; // output 60
}

What is function overloading in C++? Explain through an example?

Multiple functions having same name but the different signature is known as function overloading.

Signature only includes no of parameters and types of parameters, not include the return type.

Example:

#incoude”iostream”                        

 using namespace std;

float area(flaot red) // circle

{

 return 3.141 * red * red;

}

float area(double base, double height) // triangle

{

return 0.5 * base * height;

}

float area(flaot len , float breadth) // rectangle

{

return len * breadth;

}

main()

{

cout << area(2.0f) << endl; // area of circle

cout <<  aera(2.0, 2.0) << endl;// area of triangle

cout << area(2.0f, 2.0f) << endl;// area of rectanlge

}

What is friend function in C++? Explain through an example?

Private and protected member of a class can not be accessed through object.

If you want to access the private through the object from a function, friend function concept is required.    By declaring friend function just giving the permission to that function to access the private of the class.

 

class test
{
private:  int data;                                                             
test()
{
data = 0;
}
 test(int x)
{
data = x;
}
friend void print( test t);
{
cout << t.data << endl;
}
};
void print( test t);
{
cout << t.data << endl;
}
main()
{
test t;                                  
print(t);                                
test x(10);                                            
print(x);
}

 

basicittopic

basicittopic

A Computer Science Study for IT students and people of IT community