Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Designing Better UX For Left-Handed People

      July 25, 2025

      This week in AI dev tools: Gemini 2.5 Flash-Lite, GitLab Duo Agent Platform beta, and more (July 25, 2025)

      July 25, 2025

      Tenable updates Vulnerability Priority Rating scoring method to flag fewer vulnerabilities as critical

      July 24, 2025

      Google adds updated workspace templates in Firebase Studio that leverage new Agent mode

      July 24, 2025

      Trump’s AI plan says a lot about open source – but here’s what it leaves out

      July 25, 2025

      Google’s new Search mode puts classic results back on top – how to access it

      July 25, 2025

      These AR swim goggles I tested have all the relevant metrics (and no subscription)

      July 25, 2025

      Google’s new AI tool Opal turns prompts into apps, no coding required

      July 25, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Laravel Scoped Route Binding for Nested Resource Management

      July 25, 2025
      Recent

      Laravel Scoped Route Binding for Nested Resource Management

      July 25, 2025

      Add Reactions Functionality to Your App With Laravel Reactions

      July 25, 2025

      saasykit/laravel-open-graphy

      July 25, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Sam Altman won’t trust ChatGPT with his “medical fate” unless a doctor is involved — “Maybe I’m a dinosaur here”

      July 25, 2025
      Recent

      Sam Altman won’t trust ChatGPT with his “medical fate” unless a doctor is involved — “Maybe I’m a dinosaur here”

      July 25, 2025

      “It deleted our production database without permission”: Bill Gates called it — coding is too complex to replace software engineers with AI

      July 25, 2025

      Top 6 new features and changes coming to Windows 11 in August 2025 — from AI agents to redesigned BSOD screens

      July 25, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Helpful Built-in Functions in C++ that All Devs Should Know

    Helpful Built-in Functions in C++ that All Devs Should Know

    July 22, 2025

    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:

    1. The sqrt() Function

    2. The pow() Function

    3. The sort() Function

    4. The find() Function

    5. The binarysearch() Function

    6. The max() Function

    7. The min() Function

    8. The swap() Function

    9. The toupper() Function

    10. The tolower() Function

    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:

    // C++ program to see the use of sqrt() function
    #include <cmath>     
    #include <iostream>  
    
    using namespace std;  
    int main()
    {
        double x = 100;
    
        double answer;
    
        // Use the sqrt() function to calculate the square root of the number
        answer = sqrt(x);
    
        // Print the result 
        cout << answer << endl;
    
        return 0;
    }
    

    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:

    // C++ program to see the use of the pow() function
    #include <cmath>
    #include <iostream>
    using namespace std;
    
    int main()
    {
     // Declare an integer variable 'base' 
        int base = 5;
    
    // Declare an integer variable 'exponent' 
        int exponent = 3;
    
    // pow(5, 3) means 5^3 which is 5*5*5 = 125
    // Use the pow() function to calculate base raised to the power of exponent
        int answer = pow(base, exponent);
    
    // output the result
        cout << answer << endl;
    }
    

    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:

    #include <iostream>     
    #include <algorithm>    // Header file that includes the sort() function
    
    using namespace std;    
    
    int main()
    {
        // Declare and initialize an integer array with unsorted elements
        int arr[] = { 13, 15, 12, 14, 11, 16, 18, 17 };
    
        // Calculate the number of elements in the array
        int n = sizeof(arr) / sizeof(arr[0]);
    
        // Use the built-in sort() function from the algorithm library
        sort(arr, arr + n);
    
        // Print the sorted array using a loop
        for (int i = 0; i < n; ++i)
            cout << arr[i] << " ";  
    
        return 0;
    }
    

    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:

    // C++ program to see the the use of the find() function
    
    #include <algorithm>   // Required for the find() function
    #include <iostream>    
    #include <vector>      
    
    using namespace std;   
    
    int main()
    {
        // Initialize a vector 
        vector<int> dataset{ 12, 28, 16, 7, 33, 43 };
    
        // Use the find() function to search for the value 7
        auto index = find(dataset.begin(), dataset.end(), 7);
    
        // Check if the element was found
        if (index != dataset.end()) {
            // If found, print the position (index) by subtracting the starting iterator
            cout << "The element is found at the "
                 << index - dataset.begin() << "nd index";
        }
        else {
            // If not found
            cout << "Element not found";
       }
        return 0;
    }
    

    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:

    // C++ program for the binary_search() function
    
    #include <algorithm>   
    #include <iostream>    
    #include <vector>      
    
    using namespace std;   
    
    int main()
    {
        // Initialize a sorted vector of integers
        vector<int> arr = { 56, 57, 58, 59, 60, 61, 62 };
    
        // binary_search() works only on sorted containers
        if (binary_search(arr.begin(), arr.end(), 62)) {
            // If found, print that the element is present
            cout << 62 << " is present in the vector.";
        }
        else {
            // If not found, print that the element is not present
            cout << 16 << " is not present in the vector";
        }
    
        cout << endl;
    }
    

    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:

    // max() function
    
    #include <algorithm>  
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        // Declare two integer variables
        int a = 8 ;
        int b = 10 ;
    
        // Use the max() function to find the larger number between a and b
        int maximum = max(a, b);
    
        // Display the result with a meaningful message
        cout << "The maximum of " << a << " and " << b << " is: " << maximum << endl;
    
        return 0;
    }
    

    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:

    // use of the min() function
    
    #include <algorithm>  // For the built-in min() function
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        // Declare two integer variables to store user input
        int a = 4 ;
        int b = 8 ;
    
        // Use the min() function to find the smaller 
        int smallest = min(a, b);
    
        // Display the result 
        cout << "The smaller number between " << a << " and " << b << " is: " << smallest << endl;
    
        return 0;
    }
    

    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:

    //  use of the swap() function
    
    #include <algorithm>  // For the built-in swap() function
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        int firstNumber = 8 ;
        int secondNumber = 9 ;
    
    
        // Use the built-in swap() function to exchange values
        swap(firstNumber, secondNumber);
    
        // Display values after swapping
        cout << "After the swap:" << endl;
        cout << firstNumber << " " << secondNumber << endl;
    
        return 0;
    }
    

    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:

    // C++ program
    
    // use of tolower() function
    
    #include <cctype>     
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        // Declare and initialize a string with uppercase characters
        string str = "FRECODECAMP";
    
        for (auto& a : str) {
            a = tolower(a);
        }
    
        // Print the modified string 
        cout << str;
    
        return 0;
    }
    

    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:

    // use of toupper() function
    
    #include <cctype>     
    #include <iostream>   
    using namespace std;
    
    int main()
    {
        // Declare and initialize a string 
        string str = "freecodecamp";
    
        for (auto& a : str) {
            a = toupper(a);
        }
    
        // Output the converted uppercase string
        cout << str;
    
        return 0;
    }
    

    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 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleVPS vs PaaS: How to Choose a Hosting Solution
    Next Article Data Structure and Algorithm Patterns for LeetCode Interviews

    Related Posts

    Development

    Laravel Scoped Route Binding for Nested Resource Management

    July 25, 2025
    Development

    Add Reactions Functionality to Your App With Laravel Reactions

    July 25, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Advancing Egocentric Video Question Answering with Multimodal Large Language Models

    Machine Learning

    Threat Actors Exploiting Ivanti Connect Secure Vulnerabilities to Deploy Cobalt Strike Beacon

    Security

    Free Nextjs Landing Page Templates & Examples

    Web Development

    Best Ahrefs Alternatives Guide: 10 Tools To Choose From

    Development

    Highlights

    Development

    Accessibility vs. Inclusive Design vs. Universal Design: Understanding the Differences

    June 10, 2025

    In the push for more equitable and user-friendly experiences, three key concepts often arise: Accessibility,…

    PhpStorm 2025.1 is Here

    April 18, 2025

    CVE-2025-6659 – PDF-XChange Editor PRC File Parsing Remote Code Execution Vulnerability

    June 25, 2025

    Dove eravamo rimasti?

    April 25, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.