Function 


A function is a group of statements which takes input and produces output after performing some computations. A function returns an output when it is called. C++ supports many built-in functions (library functions) like pow(), sqrt() which can be called directly by including its library of definition. C++ also allows users to define their own functions as per their need.


Syntax:


return_type function_name (parameter_list)

{

    //body of function

}


Explanation:

  • return_type:

It specifies the type that function will return such as int, char, pointers or even class object. Functions which do not return anything are written with void.


  • function_name:

 It is the name of the function. A function is called using its name.


  • parameter_list: 

A function may or may not contain parameters. Parameters are passed when the function is called.  

  

  • body of function: 

It contains set of statements where computations are performed.                                                        



Why to use functions?


  1.  It provides reusabilty i.e. once a function is defined, it can be reused again. This reduces the size of source program and the time. 
  2. Abstraction: Library functions can be used directly without taking their internal working into consideration. Hence, it improves readability of the program.
  3.  It provides modularity. Using functions, make it easy to edit and error checking and debugging.


Function Declaration(or prototype)


It declares the properties of function to the compiler. Properties including name of function, return type of function, number of parameters and their types are declared to the compiler.


Syntax:


return_type function_name(datatype1 parameter1, datatype2 parameter2,..., datatypeN parameterN);


Example:

int func(int, char);


Note: It is not necessary to mention parameters name in function declaration, however it is mandatory in function definition.


Function Definition


It gives description about the function body. It consists of block of codes capable to perform some specific task.


Example:


int sum (int a, int b)

{

  return (a*b);

}


PARAMETERS AND ARGUMENTS


Information passed to a function are known as parameters. They can act as variables. Parameters are specified inside the parentheses of the function name. A function may not contain any parameter or may contain as much parameters as we want.



Parameters (Formal parameters): It is a variable in function declaration and function definition. It is the parameter received by a function. 


Arguments (Actual parameters): It is the actual value of the parameter. It is the parameter passed to a function.


CALLING A FUNCTION


A function must be called to perform any specific task and return an output. Functions are called by using their respective names. A function with no arguments is called directly by using its name.


Example:


//C++ program to demonstrate function calling

#include<iostream.h>

#include<conio.h>

using namespace std;

void fun();   //function declaration

void fun       //dunction definition

{

  cout<<"An example to demonstrate the calling of a function.";

}

int main()

{

  fun();         //calling function fun

  return 0;

}

OUTPUT:




Note: The same function can be called even multiple times as per our need.


Functions with arguments can be called in two ways:


1. Call by value


Here, the arguments values are copied to formal parameters and these two parameters store their values in different locations. Any changes made in formal parameters do not affect the value in actual parameter (argument). 


Example:


//C++ program to demonstrate call by value

#include<iostream.h>

#include<conio.h>

using namespace std;

int val(int);        //function declaration

int main()

{

  int a = 10;

  val(a);            //passing value to function by value

  cout<<"Value of a is: "<<a;

  return 0;

}

int val(int a)      //function definition

{

  a = a+5;

}                 //a is destroyed here

OUTPUT:



Example Explained:


In the above example, the actual value of a is passed to the function val(). Any changes made to the variable inside the called function are not reflected to the actual variable with which the function is called. On calling the function val, the actual value is produced as output. 


Note: If a function is called without any argument, it uses the default parameter value. This parameter value is used by equal signs (=).


2. Call by reference


Here, both the arguments and the parameters refer to the same memory location. Hence, any changes made in formal parameters will also affect the actual parameter (argument). In this case, the addresses are passed instead of passing values. It is useful at the time when we need change values of the arguments.


Example 1:


//C++ program to demonstrate call by reference

#include<iostream.h>

#include<conio.h>

using namespace std;

int val (int &x)       //function definition

{

  x=x+5;

}

int main()

{

  int a=10;

  val(a);       //passing value to function by reference

  cout<<"Value of a is: "<<a;

  return 0;

}

OUTPUT:




Example Explained:


Ampersand (&) in 'int val(int &x)' denotes the address of x as parameter. While passing the variable a as argument, actually, the address of x is passed instead of 10.


Example 2:

//C++ program to demonstrate call by reference using pointer

#include<iostream.h>

#include<conio.h>

using namespace std;

int val(int* );      //function declaration with pointer as parameter

int main()

{

  int a=10;

  val (&a);    //calling function by passing variable address

  cout<<"Value of a is: "<<a;

  return 0;

}

int val(int *a)      //function definition

{

  *a = *a+5;

}

OUTPUT:




Example Explained:


Here, the address of variable a is passed during the function call. Since, the address is passed, a dereference operator (*)  is used to access the value stored in that address. Hence, changes made to a are reflected in the actual value of a. 


Note: On working with multiple parameters, while calling the function, it must contain same number of arguments and the same type of values as it is in parameters. And, they must be passed in the same order.