Website For Developers

Functions in C++



  • Functions in C++ are the block of code that acts on a data on the base of statements written in it.
  • In C++, all programs have at least one function, main() function.
  • Functions are used to make the code shorter.
  • In C++, functions increase the readability of the code.
  • They make the code more efficient.

  • Advantages of functions

    Easier to Code
      Without functions the program gets very difficult and very complicated as well as lengthy. So by using functions we can make the code less complicated and less difficult.
    Easier to Modify
      By using functions, it is very easy and fast way to modify the code when needed.
    Easier to Maintain
      Functions are very easy to maintain rather than long programs.
    Reusability
      Functions in C++ are easy to use by just calling the function on a specific point.

    Types of function

    There are two types of functions:
    1. Build in Functions
    2. User Define functions

    Build in Functions
  • Build in Functions are the part of C++ library which is stored in different header files.
  • They are already defined by the language and you can approach them by the library given in C++.
  • for example getch is a build in function to hold the console screen output and it is part of a header file called conio.h


  • User defined functions
  • User-defined functions in c++ are build by the programmers.
  • User-defined functions perform tasks that are defined by the programmer.

  • Structure of function in C++

    User-defined function in C++ is consist of three parts.
      1- Function Declaration or Function Prototype
      2- Function Definition and Body
      3- Function Call

    1- Function prototype or declaration

    The working of function declaration is to provide the information about the return type, name and parameters of the function.
    If function written before main() function then it is not necessary to define prototype.

    returntype name (parametes);


    2- Function definition/body

    The set of statements that explain what a function does is called function definition.
    The statement which executes inside the function is called function body.
    The body of the function is written after the declaration of the function.
    The function definition can be written before main() function and after main() function.

    returntype name (parametes) {
      //statements in the fuctions or body of the function
    }


    3- Function Call

    To executes the body of the function, function needs to be called.

    int main() {
    .
    .
    .
    function_name(); // calling function in main()
    .
    .
    }


    Example of functions in C++

    #include<iostream>
    #include<conio.h>
    using namespace std;

    //function prototype
    void sum(int, int);

    int main() {
      int a = 50;
      int b = 50;

    // function calling
      sum(a, b);

      getch();
      return 0;
    }

    //Function definition
    void sum(int x, int y) {
      cout<< "Sum is : "<< x+y;
    }

    Output

    Sum is : 100


    Example of function without prototype

    #include<iostream>
    #include<conio.h>
    using namespace std;

    //Function definition
    int sum(int x, int y) {
      int total = x+y;
      return total;
    }

    int main() {
      int a = 33;
      int b = 22;

    // function calling
      cout<< sum(a, b);

      getch();
      return 0;
    }

    Output

    55