Built-in functions in C++ are those functions that are part of the C++ standard libraries. These functions are designed to provide common and essential functionality that is often required in programming.
In this article, we will look at some of the most commonly used built-in functions in C++ so you can start using them in your code.
What we’ll cover:
The sqrt()
Function
You use the sqrt()
function to determine the square root of the value of type double. It is defined inside the <cmath>
header file.
Syntax:
sqrt (n)
Parameter: This function takes only one parameter of type double which is a number we want to find the square root of.
Return Type: The square root of the value of type double.
Example Code
Let’s look at an example so you can see how this function works:
<span class="hljs-comment">// C++ program to see the use of sqrt() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><cmath> </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-keyword">double</span> x = <span class="hljs-number">100</span>;
<span class="hljs-keyword">double</span> answer;
<span class="hljs-comment">// Use the sqrt() function to calculate the square root of the number</span>
answer = <span class="hljs-built_in">sqrt</span>(x);
<span class="hljs-comment">// Print the result </span>
<span class="hljs-built_in">cout</span> << answer << <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
10
The pow()
Function
You use the pow()
function to find the value of the given number raised to some power. This function is also defined inside the <cmath>
header file.
Syntax:
double pow(double x, double y);
Parameters:
x: The base number.
y: The exponential power.
Return Type: value of x raised to the power y.
Example Code:
Let’s look at an example to see how this works:
<span class="hljs-comment">// C++ program to see the use of the pow() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><cmath></span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// Declare an integer variable 'base' </span>
<span class="hljs-keyword">int</span> base = <span class="hljs-number">5</span>;
<span class="hljs-comment">// Declare an integer variable 'exponent' </span>
<span class="hljs-keyword">int</span> exponent = <span class="hljs-number">3</span>;
<span class="hljs-comment">// pow(5, 3) means 5^3 which is 5*5*5 = 125</span>
<span class="hljs-comment">// Use the pow() function to calculate base raised to the power of exponent</span>
<span class="hljs-keyword">int</span> answer = <span class="hljs-built_in">pow</span>(base, exponent);
<span class="hljs-comment">// output the result</span>
<span class="hljs-built_in">cout</span> << answer << <span class="hljs-built_in">endl</span>;
}
Output:
125
The sort()
Function
The sort()
function is part of STL’s <algorithm>
header. It is a function template that you can use to sort the random access containers, such as vectors, arrays, and so on.
Syntax:
sort (arr , arr + n, comparator)
Parameters:
arr: The pointer or iterator to the first element of the array.
arr + n: The pointer to the imaginary element next to the last element of the array.
comparator: The unary predicate function that is used to sort the value in some specific order. The default value of this sorts the array in ascending order.
Return Value: This function does not return any value.
Example Code:
Let’s look at an example:
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm> // Header file that includes the sort() function</span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// Declare and initialize an integer array with unsorted elements</span>
<span class="hljs-keyword">int</span> arr[] = { <span class="hljs-number">13</span>, <span class="hljs-number">15</span>, <span class="hljs-number">12</span>, <span class="hljs-number">14</span>, <span class="hljs-number">11</span>, <span class="hljs-number">16</span>, <span class="hljs-number">18</span>, <span class="hljs-number">17</span> };
<span class="hljs-comment">// Calculate the number of elements in the array</span>
<span class="hljs-keyword">int</span> n = <span class="hljs-keyword">sizeof</span>(arr) / <span class="hljs-keyword">sizeof</span>(arr[<span class="hljs-number">0</span>]);
<span class="hljs-comment">// Use the built-in sort() function from the algorithm library</span>
sort(arr, arr + n);
<span class="hljs-comment">// Print the sorted array using a loop</span>
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i < n; ++i)
<span class="hljs-built_in">cout</span> << arr[i] << <span class="hljs-string">" "</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
11 12 13 14 15 16 17 18
The find()
Function
The find()
function is also part of the STL <algorithm>
library. You use this function to find a value in the given range. You can use it with both sorted and unsorted datasets as it implements a linear search algorithm.
Syntax:
find(startIterator, endIterator, key)
Parameters:
startIterator: Iterates to the beginning of the range.
endIterator: Iterates to the end of the range.
key: The value to be searched.
Return Value: If the element is found, then the iterator is set to the element. Otherwise, it iterates to the end.
Example Code:
Let’s look at an example to better understand how it works:
<span class="hljs-comment">// C++ program to see the the use of the find() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm> // Required for the find() function</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector> </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// Initialize a vector </span>
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> dataset{ <span class="hljs-number">12</span>, <span class="hljs-number">28</span>, <span class="hljs-number">16</span>, <span class="hljs-number">7</span>, <span class="hljs-number">33</span>, <span class="hljs-number">43</span> };
<span class="hljs-comment">// Use the find() function to search for the value 7</span>
<span class="hljs-keyword">auto</span> index = find(dataset.begin(), dataset.end(), <span class="hljs-number">7</span>);
<span class="hljs-comment">// Check if the element was found</span>
<span class="hljs-keyword">if</span> (index != dataset.end()) {
<span class="hljs-comment">// If found, print the position (index) by subtracting the starting iterator</span>
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"The element is found at the "</span>
<< index - dataset.begin() << <span class="hljs-string">"nd index"</span>;
}
<span class="hljs-keyword">else</span> {
<span class="hljs-comment">// If not found</span>
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"Element not found"</span>;
}
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
The element is found at the 3rd index
The binary_search()
Function
The binary_search()
function is also used to find an element in the range – but this function implements binary search instead of linear search as compared to the find()
function. It’s also faster than the find()
function, but you can only use it on sorted datasets with random access. It’s defined inside the <algorithm>
header file.
Syntax:
binary_search (starting_pointer , ending_pointer , target);
Parameters:
starting_pointer: Pointer to the start of the range.
ending_pointer: Pointer to the element after the end of the range.
target: Value to be searched in the dataset.
Return Value:
Returns true if the target is found.
Else return false.
Example Code:
Let’s check out an example to see how it works:
<span class="hljs-comment">// C++ program for the binary_search() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm> </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><vector> </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// Initialize a sorted vector of integers</span>
<span class="hljs-built_in">vector</span><<span class="hljs-keyword">int</span>> arr = { <span class="hljs-number">56</span>, <span class="hljs-number">57</span>, <span class="hljs-number">58</span>, <span class="hljs-number">59</span>, <span class="hljs-number">60</span>, <span class="hljs-number">61</span>, <span class="hljs-number">62</span> };
<span class="hljs-comment">// binary_search() works only on sorted containers</span>
<span class="hljs-keyword">if</span> (binary_search(arr.begin(), arr.end(), <span class="hljs-number">62</span>)) {
<span class="hljs-comment">// If found, print that the element is present</span>
<span class="hljs-built_in">cout</span> << <span class="hljs-number">62</span> << <span class="hljs-string">" is present in the vector."</span>;
}
<span class="hljs-keyword">else</span> {
<span class="hljs-comment">// If not found, print that the element is not present</span>
<span class="hljs-built_in">cout</span> << <span class="hljs-number">16</span> << <span class="hljs-string">" is not present in the vector"</span>;
}
<span class="hljs-built_in">cout</span> << <span class="hljs-built_in">endl</span>;
}
Output:
62 is present in the vector.
The max()
Function
You can use the std::max()
function to compare two numbers and find the bigger one between them. It’s also defined inside the <algorithm>
header file.
Syntax:
max (a , b)
Parameters:
a: First number
b: Second number
Return Value:
This function returns the larger number between the two numbers a and b.
If the two numbers are equal, it returns the first number.
Example Code:
Here’s an example:
<span class="hljs-comment">// max() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm> </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// Declare two integer variables</span>
<span class="hljs-keyword">int</span> a = <span class="hljs-number">8</span> ;
<span class="hljs-keyword">int</span> b = <span class="hljs-number">10</span> ;
<span class="hljs-comment">// Use the max() function to find the larger number between a and b</span>
<span class="hljs-keyword">int</span> maximum = max(a, b);
<span class="hljs-comment">// Display the result with a meaningful message</span>
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"The maximum of "</span> << a << <span class="hljs-string">" and "</span> << b << <span class="hljs-string">" is: "</span> << maximum << <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
The maximum of 8 and 10 is: 10
The min()
Function
You can use the std::min()
function to compare two numbers and find the smaller of the two. It’s also defined inside the <algorithm>
header file.
Syntax:
min (a , b)
Parameters:
a: First number
b: Second number
Return Value:
This function returns the smaller number between the two numbers a and b.
If the two numbers are equal, it returns the first number.
Example Code:
Here’s an example:
<span class="hljs-comment">// use of the min() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm> // For the built-in min() function</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// Declare two integer variables to store user input</span>
<span class="hljs-keyword">int</span> a = <span class="hljs-number">4</span> ;
<span class="hljs-keyword">int</span> b = <span class="hljs-number">8</span> ;
<span class="hljs-comment">// Use the min() function to find the smaller </span>
<span class="hljs-keyword">int</span> smallest = min(a, b);
<span class="hljs-comment">// Display the result </span>
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"The smaller number between "</span> << a << <span class="hljs-string">" and "</span> << b << <span class="hljs-string">" is: "</span> << smallest << <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
The smaller number between 4 and 8 is: 4
The swap()
Function
The std::swap()
function lets you swap two values. It’s defined inside <algorithm>
header file.
Syntax:
swap(a , b);
Parameters:
a: First number
b: Second number
Return Value: This function does not return any value.
Example:
Here’s how it works:
<span class="hljs-comment">// use of the swap() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><algorithm> // For the built-in swap() function</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-keyword">int</span> firstNumber = <span class="hljs-number">8</span> ;
<span class="hljs-keyword">int</span> secondNumber = <span class="hljs-number">9</span> ;
<span class="hljs-comment">// Use the built-in swap() function to exchange values</span>
swap(firstNumber, secondNumber);
<span class="hljs-comment">// Display values after swapping</span>
<span class="hljs-built_in">cout</span> << <span class="hljs-string">"After the swap:"</span> << <span class="hljs-built_in">endl</span>;
<span class="hljs-built_in">cout</span> << firstNumber << <span class="hljs-string">" "</span> << secondNumber << <span class="hljs-built_in">endl</span>;
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
After the swap:
9 8
The tolower()
Function
You can use the tolower()
function to convert a given alphabet character to lowercase. It’s defined inside the <cctype>
header.
Syntax:
tolower (c);
Parameter(s):
- c: The character to be converted.
Return Value:
Lowercase of the character c.
Returns c if c is not a letter.
Example Code:
Here’s how it works:
<span class="hljs-comment">// C++ program</span>
<span class="hljs-comment">// use of tolower() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><cctype> </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// Declare and initialize a string with uppercase characters</span>
<span class="hljs-built_in">string</span> str = <span class="hljs-string">"FRECODECAMP"</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span>& a : str) {
a = <span class="hljs-built_in">tolower</span>(a);
}
<span class="hljs-comment">// Print the modified string </span>
<span class="hljs-built_in">cout</span> << str;
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
freecodecamp
The toupper()
Function
You can use the toupper()
function to convert the given alphabet character to uppercase. It’s defined inside the <cctype>
header.
Syntax:
toupper (c);
Parameters:
- c: The character to be converted.
Return Value
Uppercase of the character c.
Returns c if c is not a letter.
Example Code:
Here’s how it works:
<span class="hljs-comment">// use of toupper() function</span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><cctype> </span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream> </span></span>
<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span>
</span>{
<span class="hljs-comment">// Declare and initialize a string </span>
<span class="hljs-built_in">string</span> str = <span class="hljs-string">"freecodecamp"</span>;
<span class="hljs-keyword">for</span> (<span class="hljs-keyword">auto</span>& a : str) {
a = <span class="hljs-built_in">toupper</span>(a);
}
<span class="hljs-comment">// Output the converted uppercase string</span>
<span class="hljs-built_in">cout</span> << str;
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
Output:
FREECODECAMP
Conclusion
Inbuilt functions are helpful tools in competitive programming and in common programming tasks. These help in improving code readability and enhance the efficiency of code. In the above article, we discussed some very useful common inbuilt functions. Some common inbuilt functions are max()
, min()
, sort()
, and sqrt()
, etc. By using these inbuilt libraries, we can reduce boilerplate code and speed up the process of software development. These help in writing more concise, reliable, and maintainable C++ programs.
If you enjoyed this article, you can check out more of my work here:
Ayush Mishra’s Author Profile on TutorialsPoint
And I’ve written some other tutorials about math and programming:
How to Find the Area of a Square using Python?
How to Calculate the Area of a Circle using C++?
Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & MoreÂ