!-- Global site tag (gtag.js) - Google Analytics --> function in c++ - programmingworld


function in c++

                            C++ function

 A C++  function is a named unit of a group of program statements. Functions have basically reduced the size of programs.
 A Function always returns a single value.
The Function also should have a return type.

C++ Function Definition

A Function definition is as given below.

type  function_name(parameter list)
    The Body of the function;

A  function definition must have a return statement.

A  function can also have an ‘open’ parameter list.


int data(int p)
return(p<0 ? –p: p);

C++ Function declaration

A function declaration tells the compiler about a function name and how to call the function.

A function declaration is given as below.

type function_name( parameter list );

With parameter example

int para( int x , int y);

Accessing a c++ function

When a program calls a function, program control is transferred to the called function.

It returns program control back to the main program.

To call a function, you need to pass the required parameters.


int main1()

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

float a,b;
cout << “\n Enter number= \n”;

cin >> a;
b= cube(a);                        //call the function and assign.
                                           //it’s return value to variable y.

cout << “\n The cube of” << a << “is “<< a<< “\n”;
returm 0;
}                      //end of main

float cube (float x)
float n;
 n= x*x*x*;
return (n);


Enter number
The cube of 3 is 27

Types Of Functions:-

Function are two types.

  •      Built-in functions.
  •      User-defined functions.

          1.  Built-in functions:-These functions are part of the compiler package. These are part of the standard library made available by the compiler. Example, exit(), sqrt(), pow(), strlen() etc.are library functions.

          2.    User-defined functions:- The user-defined functions are created by the user. These functions are created as per the requirement of the user program.

Function Prototype :

A function prototype is a declaration of the function that tells the program about the type of the value returned by the function and the number and type of arguments.
A function prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the type of return values.
A general form of function prototype is as shown below:

type function-name ( parameter list );

In a  function prototype, the names of the arguments are optional.

Example :

float  volume (int a, float b, float c);
float area (float, float);

Call By Value :

The call by value method copies the values of actual parameters into the formal parameters, that is, the function creates its own copy of argument values and then uses them.


int main()
int change(int);               //prototype declared

int orig1 = 10;
cout <<”\nThe value is”<< orig1 <<\n”;
cout <<”\nReturn value of function () is”<< 
change(orig) <<”\n”;
cout <<”\nThe value after function change is over” << orig <<”\n”;

return 0;
int change(int a)

a=20;                               //The value is changed.    
return a;                         //The changed value is returned.


The original value is 10
Return value of function change() is 20
The value after function change() is over 10

Call by Reference :

The call by reference method of passing arguments to a function copies the reference of an argument into the formal parameter.

Incall by the reference method changed made in the formal parameters are reflected back to actual parameters.

int main()
void swap(int &,int &);
                   //function prototype accepting two reference variable.
int a,b;
cout<<"\nThe original value are:\n";
cout<<"\n The value after swap() are\n";
return (0);
void swap(int &x,int &y)
int temp;
cout<<"\n The swapped values are:\n";


The original values are:
a=7, b=4

The swapped value are:
a=4, b=7

The values after swap() are:
a=4, b=7