Library Functions in C++

Video Tutorial
Functions thumbnail
This video belongs to
C++ Course: Learn the Essentials
14 modules


The C++ programming language contains several built-in functions to perform simple as well as complex calculations and data manipulations in user programs. The collection of these library functions in C++ is termed a Standard Library. We can also define our functions in the program, and they are called user-defined functions.

The C++ Standard Library Header Files

The library contains the function implementation, and by including the header file for the corresponding library, we can use the required function in our program. The tabular description of some of the standard library header files is presented below.

C++ Standard Library Header FileDescription
< cstdlib >This library consists of several general-purpose functions like conversion related, sequence generation related, dynamic memory management related, etc.
< ctime >It contains functions to get and manipulate the date and time.
< iomanip >This library contains IO manipulators that are used to format the stream of data.
< string >This library contains the function and classes that are useful to work with the string in C++.
< cstring >It consists of the string handling functions for c-styled strings.
< climits >This library defines the constant for the fundamental integral types. We generally use these constants for comparisons.
< vector > < list > < deque > < queue > < stack > < map > < set > < bitset >These all header files are corresponding to the container of the standard template library of C++. Each container exhibits unique properties and is used to store data. The implementation of the data type is written in the associated library.
< iterator >It contains classes and functions related to iterators that help us in accessing the data of containers.
< algorithm >It provides various general purpose algorithms to operate on the containers.
< utility >This is the normal utility library which provides various functions in unrelated domains.
< functional >The functionality provided by this library is used by the algorithm library.
< exception >It contains classes for exception handling and other related functions to assist the error handling.
< iostream >As the name suggests, this library contains functions and streams for standard input and standard output in C++.
< cassert >It contains macros and functions that are used in the debugging.

Important Mathematical Functions in Header file <cmath>

The cmath header file contains various advantageous functions of several categories to perform straightforward as well as complicated mathematical calculations. Below are some of the library functions in C++ which are declared in the cmath header file,

1. Trigonometric Functions

  • double sin (double x): This method accepts angle in radians and returns the sine of it.

  • double tan (double x): This method accepts angle in radians and returns the tangent of it.

2. Power, Exponential and Logarithmic Functions

  • double exp (double x): It accepts a numerical value and returns the base e exponential value of that.

  • double pow (double base, double exponent): This method accepts base and exponent and then simply returns Base to the power exponent.

  • double log (double x): It computes the natural logarithmic value of provided parameter.

  • double sqrt (double x): It computes the square root of the provided parameter.

3. Rounding and Remainder Functions

  • double floor (double x): It returns an integral value which is nearest as well as not more to the provided number.

  • double trunc (double x): This method truncates the digits after the decimal point in the floating point numbers.

  • double round (double x): It returns the nearest integral value corresponding to the provided number.

  • double remquo (double numer, double denom, int *quot): It computes the remainder and quotient of any division and stores the quotient in the passed memory address.

4. Minimum, Maximum and Difference Functions

  • double fdim (double x, double y): It returns an positive difference between the provided number but only if the first parameter x is greater than other y, otherwise it will return 00.
  • double fmin (double x, double y): It returns the minimum of both provided numbers as parameter
  • double fmax (double x, double y): Similar to the last one it returns the maximum of the provided numbers as parameter.

Character Functions <cctype>

This library contains various functions that operate on the characters to either classify them or perform some sort of conversion. Below given some of the library functions in C++ which are declared in the cctype header file,

1. Character Classification Functions

There are several function which classifies the character, some of them are shown below,

  • bool isalpha ( char c ): It accepts a character and returns false, if it is not a alphabet otherwise it returns true.
  • bool isdigit ( char c ): Similar to the last one, it accepts a character and returns false, if that is not a digit otherwise, it returns true.
  • bool isupper ( char c ): This library function in C++ returns false, if the provided character is not in uppercase, otherwise it returns true.
  • bool islower(char c) : This library function in C++ returns false, if the provided character is not in lowercase, otherwise it returns true.

2. Character Conversion Functions

There are two functions which can convert the character from lowercase to uppercase and vice versa.

  • char tolower ( char c ) It returns a value equivalent to lowercase of provided character, if no such character exists it returns the same character.
  • char toupper ( char c ), Similar to last one, it returns a value equivalent to uppercase of provided character, if no such character exists it returns the same character.

Function Prototype

The function prototype is the description of the function which contains the information about the name of the function, its parameter, and return value.

Generally, it is used to declare the functions so that the compiler can know that there is a function_name defined later in the program, which accepts the argument and returns a value of some data type as described in the prototype.

It is also useful so that the programmer who is using the library function in C++ can get to know about the information like what is the input(argument) and output(return value) of function_name.

Function Definition

The functions are the chunk of code that is used repeatedly according to requirements, so the actual code of the function written inside the curly braces of C++ is called the function definition. This definition is the thing that is responsible for the processing of user input and producing appropriate output.

User Defined Functions

Apart from the few library functions in C++, We can also define our own function to perform several tasks. In this section, you will learn about how we can create our functions and what are their advantages.

Syntax for Using Functions in C++

The syntax of the user-defined function is as follows,

  • First, we write the return type.
  • Then the function name followed by the argument wrapped inside the parenthesis.
  • After all of this, there exist the definition of function inside the curly braces.

Declaration of the Function.

As we know, C++ is a procedural programming language, so if the main function is written before our user-defined function, then during the call, C++ won't be able to recognize the function. We have to explicitly declare it before using it.

Call with Appropriate Arguments.

When the function is already declared and defined, we call the function by writing the function's name followed by the arguments wrapped inside the opening and closing parenthesis, respectively.

The passed argument must be compatible according to the function prototype. Otherwise, the program will throw an error. If we provide less than or more than two parameters to function, it will cause an error stating too few arguments or too many arguments.

Also, the data type of arguments should be either same or compatible for implicit typecast.

Types of User-defined Functions

The user-defined function can be classified into four categories according to their parameter and return values.

  1. Function with no argument and no return value

The function that neither accepts any argument nor provides a return value comes under this category.

Now, as we know, the functions are used to process some input and subsequently provide output, So a question arises "Why are we using such a function which is neither receiving input nor returning some output"? The reason is apart from the parameters and return value, there could be two major scenarios to accept input and provide output.

  1. Ask for the input directly from stdin or access the memory location associated with the variable outside the function scope.

  2. Provide output directly to stdout or access any memory location associated with the variable outside the scope to store the output.



  1. Function with no argument but return value

This type of function doesn't accept any argument but returns a value.



  1. Function with Argument but No Return Value

The function which accepts argument but doesn't returns any value.



  1. Function with Argument and Return Value

The function which accepts argument as well as returns a value falls into this category. Basically this is the most used form of functions.



Function Arguments

  1. Call by Value

The actual value is passed to the function, and according to this, a copy of the variable is created inside the function scope, so if the function manipulates that variable the changes will only appear in the function scope. library function




  • When we passed the num1 and num2 to the function as values, the 5 and 14 were copied to those formal arguments.
  • Due to this, copy the swapping is being seen in the function but not outside that.
  1. Call by Pointer

The pointer to the memory is passed to the function. With this technique, the function can manipulate the data associated with that pointer. As with pointers, we can directly access the variable, so the changes made by the pointer will appear in the original scope. library fun



  • We have created two integers in the main function, which are being passed to a function named swap.
  • The function accepts the address of the variables in pointer variables.
  • Inside the function definition, we have swapped the value of the variables which was associated with the provided address.
  1. Call by Reference

Reference is somewhat similar to the pointer technique. The term reference refers to some existing variable, so it is just an alias for a variable.

The manipulations made in the reference variable will also appear in the original variable.



  • The pointer and reference work somewhat in a similar way.
  • We are passing arguments from the function call, and the function is receiving the reference as formal argument..
  • The changes made by the function will be reflected in the original variable because the received ones were just a reference to the original ones.

Advantages of User-Defined Functions

  • Code Readability: The functions enhance the code readability because they are pretty straightforward; receive input, process, and provide output. It is much easier to understand something bit by bit instead of the whole.
  • Modularity: The modularity term explicates the separateness of decoupled stuff of the system. With the function, we can easily separate the entire code into several components, making it easier to code and maintain the program..
  • Easy to Debug: As our program is now divided into several functions, it is uncomplicated to debug it. We can provide a set of inputs to each function and compare the correct output with the one provided by the function. If both are equal, we can conclude that function is working fine.
  • Write once Use Everywhere: We can define our function once and use it anywhere inside the program by imparting a different set of inputs. It avoids a lot of code repetition in the program. Suffice to say, functions are also helpful for the compactness of software.


  • C++ has several useful library functions, and we can utilize them by including the corresponding header files in our programming
  • The function prototype is the description of the function. It delivers information about the parameter and the return value.
  • The function definition refers to the actual code dedicated to processing the input to yield useful output.
  • In the user-defined function, along with the definition, the function call is necessary to use that function. We have three major ways to call the function, i.e., by value, reference, pointer, etc.
  • The benefits of the functions are modularity, simple and straightforward coding pattern, uncomplicated debugging, avoidance of code repetition, etc.

Learn More: