functions in c++

Introduction -: We know that function play an important role in C program development. Dividing a program into function is one of the major principle of top down structured programming. Other advantage of function is that it is possible to reduce the size of a program by calling and using them at different place in the program.

Recall that we have used a syntax similar to the following in developing c programming…

void show();  /* function declaration */

main()
{

..........
show                /* function call*/
.......

}

void show ()            /* function definition */

{ 
 .......
 .......              /* function body */
 .......
}

 

when the function is called control is transferred to the first statement  in the function body. The other statement in the function body are then execute and control return the main program  when the closing braces is encountered. C++ us no exception. functions continue to be the building blocks of c++ programs.

In the c++ programming has added many new features to function to make them more reliable and flexible.

Types of function in c++ programming

  • The main function 
  • Function prototyping 
  • Call by reference
  • Return by reference 
  • Inline function 
  • Default arguments
  • Const arguments 
  • Friend and virtual function

 

The Main Function -:  C does not specify any return type for the main function which is the starting point for the execution program.  Its looks like..

main()

{
     // main program statement

}

In c++ the main() return a value of type int to the operating system.

Function Prototyping -: It is one of the major improvement added in c++ programming. It’s describing the function interface to the compiler by giving details such as the number and types arguments and the type of return value. With function prototyping a template is always used when declaring and defining function. when a function is called the compiler used the template to ensure that proper arguments are passed and the return value is treated correctly.

Function prototyping is a declaration statement in the calling program and is of the following like …

type function-name( argument-list)

the arguments list contains the types and name of arguments that must be passed to the function. For example

float volume(int x,float y,float z)

 

Call by Reference-: C++ permit us to pass parameters to the function by reference. When we pass arguments by reference the formal arguments in the called function become aliases to the actual arguments in the calling function. This means that when the function is working with its own arguments it is actually working on the original data like this …

void swap(int &a, int &b)
{
      int t=a;
      a=b;
      b=t;
}

If the p and q are two integer variable then the function call

swap (p,q)

will exchange the value of p and q using there alias a and b.

void swap(int *a, int *b)
{
    int t;
    t=*a;
    *a=*b;
    *b=t;
}

 

Return by reference -:  A function can also return a reference. for like

int & max (int &a, int &b)
{
  if (a>b)

       return a;

  else
 
       return b;
}

In the return type of max() is int &. the function return reference to a and b . Then a function call such as max (c,d)will yield a reference to either c or d on depending their value.this means that function call can appear on the left hand side of an assignment statement.

 

Leave a Reply

Your email address will not be published. Required fields are marked *