Functions in C++

Introduction

  • Function is a block of code that performs some operation.
  • It is a self-contained block of statements that perform a task.
  • Function is used to break down the complex program into the smaller chunks.
  • It is useful for encapsulating common operations in a single reusable block that clearly describes what the function does.
  • It defines input parameters that enable callers to pass arguments into the function and returns a value as output.
Syntax:
return_type function_name (argument_list)
{
     //Statements;
}

Following are the parts of a function:

return_typeIt is a data type that function returns. It is not necessary that function will always return a value.
function_nameFunction name is the actual name of the function.
argument_list / parametersIt allows passing arguments to the function from the location where it is called from. The argument list is separated by comma.
Function bodyIt contains a collection of statements that define what the function does.

Example : Program demonstrating the Function

#include <iostream>
using namespace std;
int addition(int no1, int no2);                          //Function declaration
int main()
{
     int num1=10, num2=20, result;                  //Local variable
     result = addition(num1,num2);        //Calling function. num1 & num2 are Actual Parameters
     cout<<"Addition is : "<<result<<endl;
     return 0;
}
int addition(int no1, int no2)     //Function definition. no1 & no2 are Formal Parameters
{
     int disp;
     disp=no1+no2;
     return disp;
}


Output:
Addition is : 30

Recursive Functions

  • A function that calls itself is called as a Recursive function.
  • It is useful for solving mathematical problems like calculating factorial of a number, fibonacci series etc.

Example: Program demonstrating Recursive function

Calculating the factorial of a number using recursive function.

#include <iostream>
using namespace std;
int factor (int);
int main()
{
     int x = 5,fact;
     fact = factor(x);
     cout<<"Factorial is : "<<fact<<endl;
     return 0;
}
int factor (int y)
{
    int a;
    if (y == 1)
        return 1;
    else
        a = y*factor(y-1);
        return (a);
}


Output:
Factorial is : 120

Call by Value and Call by Reference

Following is the difference between Call by Value and Call by Reference:

Call by ValueCall by Reference
It is the method of passing arguments to a function that passes the actual value of an argument into the formal parameter of the function.It is the method of passing arguments to a function that passes the reference of an argument into the formal parameter.
Original value cannot be changed or modified in call by value.Original value is changed or modified in call by reference.
It passes value to the function.It passes address of the value to the function.
Actual and formal parameters will be created at different memory location.Actual and formal parameters will be created at same location.
In call by value, changes made to the parameter inside the function have no effect on the argument.In call by reference, changes made to the parameter affect the argument, because address is used to access the actual argument.
Example

#include <iostream>
using namespace std;
void swap(int a, int b)
{
    int temp;
    temp = a;
    a = b;
    b = temp;
}
int main()
{
    int a = 10, b = 20;
    swap(a, b);  //passing value to function
    cout<<"Value of a : "<<a<<endl;
    cout<<"Value of b : "<<b<<endl;
    return 0;
}

Output

Value of a : 10
Value of b : 20
Example

#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
int main()
{
    int a = 10, b = 20;
    swap(&a, &b);    //passing value to function
    cout<<"Value of a : "<<a<<endl;
    cout<<"Value of b : "<<b<<endl;
    return 0;
}

Output

Value of a : 20
Value of b : 10

Inline Function

  • A function defined in the body of a class declaration is an inline function.
  • Inline function is a combination of macro and function.
  • It is a powerful concept in C++ programming language.
  • This function increases the execution time of a program.
  • Inline is a request to the compiler. It is an optimization technique used by the compiler.
  • The keyword inline is used before the function name to make function inline.
Syntax:
inline function_name()
{
     //Function body
}

Example : Demonstrating the Inline function execution

#include <iostream>
using namespace std;
inline void display()
{
     cout<<"Welcome to TutorialRide";
}
int main()
{
     display();  // Call it like a normal function
}


Output:
Welcome to TutorialRide

Where does the Inline function not work?
  • Inline function does not work if the functions are recursive.
  • Inline function is not used when the function contains static variables.
  • Inline function does not return any value even if the return statement is exists in the function.
Advantages of Inline Function
  • Inline function does not require calling function overhead.
  • It makes the program faster.
  • Inline function increases locality of reference by utilizing instruction cache.
  • It saves overhead of return call from a function.
Disadvantages of Inline Function
  • Inline function increases function size so that it may not fit in the cache and causes lots of cache miss.
  • If Inline function is used in the header files, it increases the header file size and makes it unreadable.
  • Inline function is not useful for embedded system where large binary size is not preferred due to memory size constraints.